0% found this document useful (0 votes)
6 views

Developing Web Applications Using Microsoft Visual Studio 2008

This document provides an overview of user input validation in ASP.NET applications using Microsoft Visual Studio 2008. It discusses client-side and server-side validation, and the various validation controls available in ASP.NET like RequiredFieldValidator, CompareValidator, RangeValidator, and CustomValidator. It also describes implementing the ValidationSummary control to collect and display validation error messages on a page. The accompanying lab exercises demonstrate how to use these validation controls to validate user input fields.
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)
6 views

Developing Web Applications Using Microsoft Visual Studio 2008

This document provides an overview of user input validation in ASP.NET applications using Microsoft Visual Studio 2008. It discusses client-side and server-side validation, and the various validation controls available in ASP.NET like RequiredFieldValidator, CompareValidator, RangeValidator, and CustomValidator. It also describes implementing the ValidationSummary control to collect and display validation error messages on a page. The accompanying lab exercises demonstrate how to use these validation controls to validate user input fields.
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/ 21

Developing Web

Applications Using
Microsoft® Visual
Studio® 2008
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
version
Valid?
 Instant feedback No
Client Yes
 Reduces postback cycles
Server
• Server-side validation
Valid?
 Repeats all client-side No
validation Yes
 Validates against stored
data Web Application
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:ControlType id="ID of control" runat="server" />

<asp:ValidatorType id="ID of validator"


runat="server"
ControlToValidate="ID of control"
ErrorMessage="Error message for error summary"
Display="Static, Dynamic or None"
Text="Text to display next to the input control">
</asp:ValidatorType>
Positioning Validation Controls on a Web Form
• Create error messages

• Select display mode


 Static

 Dynamic

Position changes
Combining Validation Controls
• Can have multiple validation controls on a single input
control
• Only the RequiredFieldValidator checks empty controls
Input Validation Controls
• RequiredFieldValidator • CompareValidator
 InitialValue  ValueToCompare or
ControlToCompare

• RangeValidator  Type

 MinimumValue  Operator

 MaximumValue
 Type

<asp:RequiredFieldValidator
<asp:RangeValidator
<asp:CompareValidator
id="ageRangeValidator"
id="pwdCompareValidator"
id="nameRequiredValidator"
runat="server"
ControlToValidate="ageTextBox"
runat="server"
runat="server" Type="Integer"
MinimumValue="18"
ErrorMessage="These
ControlToValidate="nameTextBox"
fields do not match"
MaximumValue="50"
ControlToCompare="pwd1TextBox"
InitialValue="Enter your name"
ErrorMessage="Applicants
ControlToValidate="pwd2TextBox"
ErrorMessage="You must enter
must your
be between
name" 18-50"
Display="dynamic"
Text="*">
Display="dynamic" Text="*">
Text="*">
</asp:RequiredFieldValidator>
</asp:RangeValidator>
</asp:CompareValidator>
RegularExpressionValidator Control
• Verifies that user input matches a predefined pattern

• Visual Studio 2008 includes patterns for:


 Telephone numbers
 Postal codes
 E-mail addresses
<asp:TextBox id="emailTextBox" runat="server" />

<asp:RegularExpressionValidator
id="emailRegexValidator"
runat="server"
ControlToValidate="emailTextBox"
ErrorMessage="Use the format
[email protected]"
ValidationExpression="\w+@\w+\.\w+"
Text="*">
</asp:RegularExpressionValidator>
CustomValidator Control
• Can validate on client-side, • Validate with:
server-side, or both
 Formula  COM objects
 ClientValidationFunction
 Data  Web Service
 OnServerValidate

<asp: CustomValidator...
ClientValidationFunction = "MyClientFunction"
[Visual
OnServerValidate
C#]
Basic] = "MyServerFunction" />
private
Sub void MyServerFunction(object
MyServerFunction(sender as Object, sender,
_
ServerValidateEventArgs
e
<script as e) {
ServerValidateEventArgs)
language = "Jscript">
int MyClientFunction(source,
Dim
function = Convert.ToInt16(e.Value);
value As Integer = e.Value
arguments) {
if value
If (value%2
alert("I am mod == 0)on
2 =
running 0{Then
the client! ");
e.IsValid
var value true;
= True
= arguments.Value;
if
} (value
else { % 2 == 0) {
Else
arguments.IsValid = true;
false;
e.IsValid = False
} else {
}
End If
arguments.IsValid = false;
} }Sub
End
}
</script>
Demonstration: Implementing the
CustomValidator Control
• Add a CustomValidator control

• Write the server-side code

• Write the client-side code

• Test the result


Lesson: Page Validation
• The Page.IsValid Property

• Implementing the ValidationSummary Control


The Page.IsValid Property

Polls all validation controls

[Visual C#]
protected void submitButton_Click(object sender, EventArgs e)
{
if (Page.IsValid) {
// Page valid, perform database updates or logic
...
}
}

[Visual Basic]
Protected Sub submitButton_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
If Page.IsValid Then
' Page valid, perform database updates or logic
...
End If
End Sub
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="myValidationSummary"
runat="server"
HeaderText="These errors were found:"
ShowSummary="True"
DisplayMode="List"/>
Lab: 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-LON-DEV-06


User name Student
Password Pa$$w0rd

Estimated time: 25 minutes


Lab Scenario

Master Page
Logon Page benefitsMaster.master
login.aspx
Benefits Lab Web
Home Page Application
ASPState
Default.aspx Page Header
header.ascx
Menu Component
Registration Benefits.cs or Benefits.vb
register.aspx TempDB
Web.
config

Life Insurance Retirement Medical Dentists


life.aspx retirement.aspx medical.aspx dental.aspx

Prospectus
prospectus.aspx Doctors User Control XML Web
LINQ to SQL doctors.aspx nameDate.ascx Service
Classes DentalService1.asmx
Doctors.dbml

Doctors Dentists
XML Files
Lab Review
Review Questions
• What are the properties that you should set for a
RequiredFieldValidator control?
• What are the properties that you should set for a
ValidationSummary control?
• What are the properties that you should set for a
CompareValidator control?
• What are the properties that you should set for a
RegularExpressionValidator control?
Module Review and Takeaways
• Review Questions

• Real-World Issues and Scenarios

• Best Practices

You might also like