Full source code is provided in Listings 1-15 and 1-16. The web form has TextBox controls to test
input and a Label to display the success or failure of the web form postback according to the
validation process.
Listing 1-15. The ValidationControls Web Form .aspx File
<%@ Page Language="C#"
MasterPageFile="~/MasterPage/ControlsBook2MasterPage.Master"
AutoEventWireup="true" CodeBehind="ValidationControls.aspx.cs"
Inherits="ControlsBook2Web.Ch01.ValidationControls"
Title="Validation Controls Demo" %>
Width="14px">1 ID="ChapterTitleLabel" runat="server" Width="360px">
Server Control Basics and What's new in ASP.NET
Table 1-3. Validation Controls Available in ASP.NET
Validation Control Description
RequiredFieldValidator Checks for a null or empty value in a server control
CompareValidator Compares two server controls by various operators
RangeValidator Ensures the values of a server control fall in a specific range
RegularExpressionValidator Uses a regular expression to validate the input of a server control
CustomValidator Allows the programmer to specify client-side and server-side
validation routines to constrain a server control??™s input
ValidationSummary Shows a summary of all the error messages generated by
validator controls on a web form
36 CHAPTER 1 ?– SERVER CONTROL BASICS
RequiredField
ErrorMessage="RequiredField needs an input value!"
ControlToValidate="RequiredField">
ComparedField
ErrorMessage="RequiredField and ComparedField are not equal!"
ControlToValidate="ComparedField"
ControlToCompare="RequiredField">
RangeField
ControlToValidate="RangeField" MaximumValue="10" MinimumValue="1"
Type="Integer">
RegexField (Phone)
ErrorMessage="RegexField must be a valid US phone number!"
ControlToValidate="RegexField" ValidationExpression=
"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}">
CustomField (Even Number)
"CustomField must be an even number!"
ControlToValidate="CustomField" ClientValidationFunction="ValidateEvent"
OnServerValidate="ValidateEvent">
OnClick="ValidateButton_Click">
runat="server">
CHAPTER 1 ?– SERVER CONTROL BASICS 37
Listing 1-16.
Pages:
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88