Developing Web Applications Using Microsoft Visual Studio 2008
Developing Web Applications Using Microsoft Visual Studio 2008
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?
• Compare to a range
• RegularExpressionValidator Control
• CustomValidator Control
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
<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
[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
<asp:ValidationSummary id="myValidationSummary"
runat="server"
HeaderText="These errors were found:"
ShowSummary="True"
DisplayMode="List"/>
Lab: Validating User Input
• Exercise 1: Implementing RequiredFieldValidator Controls
Logon information
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
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
• Best Practices