Validating User Input
Validating User Input
• Validation Controls
• Page Validation
Lesson: Overview of User Input Validation
• What Is Input Validation?
• Avoids spoofing
or the addition of
malicious code
Client-Side and Server-Side Validation
• Server-side validation
Valid?
No
Repeats all client-side Yes
validation
Web Application
Can validate against stored
data Processed
ASP.NET Validation Controls
ASP.NET provides validation controls to:
• Compare values
• Compare to a range
• RegularExpressionValidator Control
• CustomValidator Control
<asp:Type_of_Validator
id="Validator_id"
runat="server"
ControlToValidate="txtName"
ErrorMessage="Message_for_error_summary"
Display="static|dynamic|none"
Text="Text_to_display_by_input_control">
</asp:Type_of_Validator>
Positioning Validation Controls on a Web Form
Dynamic
Combining Validation Controls
• Can have multiple validation controls on a single input control
• CompareValidator
ValueToCompare or ControlToCompare
Type
Operator
• RangeValidator
MinimumValue
MaximumValue
Type
Code Examples
RegularExpressionValidator Control
• Used when input must conform to a pre-defined
pattern
• Visual Studio .NET includes patterns for:
Telephone numbers
Postal codes
E-mail addresses
Code Example
CustomValidator Control
• Can validate on client-side, server-side, or both
ClientValidationFunction
OnServerValidate
• Validate with:
Formula
Data
COM objects
Web Service
Code Example
Demonstration: Implementing the
CustomValidator Control
• Add a CustomValidator control
<asp:ValidationSummary id="valSummary"
runat="server"
HeaderText="These errors were found:"
ShowSummary="True"
DisplayMode="List"/>
Validating User Input
• Exercise 1: Implementing RequiredFieldValidator Controls
Logon information
Virtual machine 2310C_06
User name Student
Password Pa$$w0rd
Logon Page
Login.aspx
Benefits
Coho Home Page Page Header ASPState
Winery Default.aspx Header.ascx
Menu
Registration Component
Register.aspx Class1.vb or Class1.cs Web.
tempdb
config
XML
Doctors Dentists
Files
Lab Review
Module Review and Takeaways
Review Questions
• Given the following user input fields, what kind of
validation control(s) would you use?
The user’s age
The user’s telephone number
The user’s password, which is entered twice
Whether an entered number is prime
Whether all of the fields in a form are correctly filled in
Whether a new employee.s requested e-mail address matches
the company policy
Review for Alpha
• Is there any topic or specific content item in the module
that seemed unclear or unnecessary?
• Is there any content item/related subject area that was
not covered and could be included?
• Did you observe any issues with the technical accuracy of
the content?
• Is the content in the module presented in a manner that
encourages learning? Did the flow of topics seem right?
• Does the lab outline indicate the expected scope of tasks
to be covered? Would you like to suggest any tasks that
could be removed or added?