0% found this document useful (0 votes)
17 views22 pages

Validating User Input

Here are my suggestions for improving the module: - Add more detailed code examples for each validation control to improve clarity. - Include a summary of best practices for validation (e.g. validate on server-side as well). - Consider breaking the module into smaller sub-modules organized by validation type for better flow. - The lab tasks seem appropriately scoped. Adding a task on custom validation functions could help reinforce that content. Overall the module content is technically accurate and covers the key topics for user input validation in ASP.NET. With some restructuring and additional examples, it could be an even more effective learning resource. Please let me know if any other feedback would be helpful.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views22 pages

Validating User Input

Here are my suggestions for improving the module: - Add more detailed code examples for each validation control to improve clarity. - Include a summary of best practices for validation (e.g. validate on server-side as well). - Consider breaking the module into smaller sub-modules organized by validation type for better flow. - The lab tasks seem appropriately scoped. Adding a task on custom validation functions could help reinforce that content. Overall the module content is technically accurate and covers the key topics for user input validation in ASP.NET. With some restructuring and additional examples, it could be an even more effective learning resource. Please let me know if any other feedback would be helpful.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

Validating User Input

Module 6: Validating User Input


• Overview of User Input Validation

• Validation Controls

• Page Validation
Lesson: Overview of User Input Validation
• What Is Input Validation?

• Client-Side and Server-Side Validation

• ASP.NET Validation Controls


What Is Input Validation?
• Verifies that a control value is correctly entered by the user

• Blocks the processing of a page until all controls are valid

• Avoids spoofing
or the addition of
malicious code
Client-Side and Server-Side Validation

• ASP.NET can create both


client-side and server-side User Enters
validation Data Error
• Client-side validation Message

 Dependent on browser Valid?


version No
 Instant feedback Client Yes
 Reduces postback cycles Server

• 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 custom formula

• Compare to a range

• Compare to a regular expression pattern

• Require user input

• Summarize the validation controls on a page


Lesson: Validation Controls
• Adding Validation Controls to a Web Form

• Positioning Validation Controls on a Web Form

• Combining Validation Controls

• Input Validation Controls

• RegularExpressionValidator Control

• CustomValidator Control

• Demonstration: Implementing the CustomValidator


Control
Adding Validation Controls to a Web Form

11 Add a validation control

22 Select the input control to validate

33 Set validation properties

<asp:TextBox id="txtName" runat="server" />

<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

• Create error messages

• Select display mode


 Static

 Dynamic
Combining Validation Controls
• Can have multiple validation controls on a single input control

• Only the RequiredFieldValidator checks empty controls


Input Validation Controls
• RequiredFieldValidator
 InitialValue

• 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

• Write the server-side code

• Write the client-side script

• Test the result


Lesson: Page Validation
• The Page.IsValid Property

• Implementing the ValidationSummary Control


The Page.IsValid Property
Polls all validation controls
Sub cmdSubmit_Click(s As Object, e As EventArgs)
Page.Validate()
If Page.IsValid Then
Message.Text = "Page is valid!"
' Perform database updates or other logic here
End If
End Sub

private void cmdSubmit_Click(object s, System.EventArgs e)


{
Page.Validate();
if (Page.IsValid)
{ Message.Text = "Page is Valid!";
// Perform database updates or other logic here
}
}
Implementing the ValidationSummary Control
• Collects error messages from all validation controls on the
page
• Can display text and error messages

• Use Text="*" to indicate the location of the error

<asp:ValidationSummary id="valSummary"
runat="server"
HeaderText="These errors were found:"
ShowSummary="True"
DisplayMode="List"/>
Validating User Input
• Exercise 1: Implementing RequiredFieldValidator Controls

• Exercise 2: Implementing the ValidationSummary Control

• Exercise 3: Implementing the CompareValidator Control

• Exercise 4: Implementing the RegularExpressionValidator


Control

Logon information
Virtual machine 2310C_06
User name Student
Password Pa$$w0rd

Estimated time: xx minutes


Lab Scenario

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

Life Insurance Retirement Medical Dental


Life.aspx Retirement.aspx Medical.aspx Dental.aspx

Prospectus Doctors User Control XML Web


Lab Web Prospectus.aspx Doctors.aspx namedate.ascx Service
Application dentalService1.asmx

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?

You might also like