Validating User Input With Validation Controls
Validating User Input With Validation Controls
Controls
Objectives
The various classes of input validation
How to use the RequiredFieldValidator to ensure that the user has provided input
How to use the CompareValidator
How to ensure that the user’s input falls between a range of values by using the
RangeValidator
How to use the RegularExpressionValidator
Input validation is the process of ensuring that the data entered by a user is in the proper format
and/or meets certain constraints.
1|Page
Certain types of string input must conform to a particular format. For example, mailing addresses
in the United States include a ZIP code, which is denoted using either XXXXX or XXXXX-
XXXX where X is a digit.
In ASP.NET, validation is performed through the use of Web controls. The Web controls that
perform input validation are commonly called validation Web controls, or just validation
controls.
We will create a page that collects the following information from users:
Name, which is a required field
Age, which is a numeric field that must be between 0 and 150
Social Security number (SSN), which is a string input with the format XXXXX-XXXX,
where X is a digit
Number of children, which must be greater than or equal to 0
Number of male children, which must be greater than or equal to 0 and less than or equal
to the number of total children
Set the ID properties for these five TextBox Web controls.
Set the first TextBox Web control’s ID property to Name;
the second’s to Age;
the third’s to SSN;
2|Page
the fourth’s to TotalChildren; and
the fifth’s to MaleChildren.
For the Age, TotalChildren, and MaleChildren TextBox Web controls, also set the
Columns property to 4.
Next, add a Button Web control beneath the five TextBox Web controls. Set the Button’s ID
property to SubmitButton and its Text property to Click Me.
Finally, add a Label Web control below the Button, clearing out its Text property and setting its
ID to Results.
Your screen should now look similar to the following figure
3|Page
All validation Web controls contain a ControlToValidate property that specifies the ID of the
input Web control to be validated.
To require users to provide a value in the Name TextBox control, we need to add a
RequiredFieldValidator to the page and set its ControlToValidate property to Name.
It is important to understand that each validation Web control added to an ASP.NET page can
validate only one input Web control. Therefore, if the ValidationControlTestBed.aspx page had
three required input fields (say, Name, Age, and SSN), we would need to add three
RequiredFieldValidator controls to the page. The first RequiredFieldValidator control would
have its ControlToValidate property set to Name, the second’s to Age, and the third’s to SSN.
For our example, Name is the only required input Web control; therefore, we only need the one
RequiredFieldValidator on the page.
Set the ControlToValidate property of the RequiredFieldValidator that we just added to
the page to Name.
o To do this, select the RequiredFieldValidator so that its properties are loaded in
the Properties
window.
Next, click the ControlToValidate property, which will show a drop-down list of the
various input Web controls on the page. Select the Name option from the list.
Specifying What Error Message to Display for Invalid Input Along with a
ControlToValidate property, all validation controls contain an ErrorMessage property.
This string property contains the text that is displayed when the user’s input fails to meet
the validation requirements. Typically, this text should provide a brief explanation as to
the problem with the user’s input and what she needs to do to fix it.
4|Page
Equal
NotEqual
GreaterThan
GreaterThanEqual
LessThan
LessThanEqual
DataTypeCheck
Because we want to ensure that the number of total children entered by users is greater
than or equal to 0, set the Operator property to GreaterThanEqual.
In addition to the Operator property, we need to set the Type property. The Type property
indicates what data type the users’ input should be provided in. The Type property can be set
to one of the following data types:
String
Integer
Double
Date
Currency
Because we want the user to enter the total number of children as a numeric value
without a decimal, set the Type property to Integer.
Specify the value we want to compare the user’s input to. This value can be either a constant
value or the value entered by the user in some other input Web control. For the TotalChildren
TextBox control, we want to ensure that the user’s input is greater than or equal to a constant
value, namely 0.
Therefore, set the Compare Validator’s ValueToCompare property to 0.
Set the ControlToValidate and ErrorMessage properties.
Set the ControlToValidate property to TotalChildren and the ErrorMessage property to a
descriptive message, such as The total number of children must be a whole number
greater than or equal to 0.
Add a CompareValidator to ensure that the value entered into the number of male
children input is less than or equal to the total number of children input.
Drag a CompareValidator from the Toolbox onto the page, placing it to the right of the
MaleChildren TextBox.
o Set the CompareValidator’s ControlToValidate property to MaleChildren,
o Operator property to LessThanEqual,
o Type to Integer,
5|Page
o ErrorMessage to The number of male children must be less than or equal to the
number of total children., and
o ControlToCompare property to TotalChildren.
Add a RangeValidator to the right of the Age TextBox Web control. As with
RequiredFieldValidator and CompareValidator,
Set the RangeValidator control’s ControlToValidate and ErrorMessage properties.
Set the ControlToValidate property to Age and the ErrorMessage property to Age must
be between 0 and 150.
Because we want the user to enter his age as a number without decimals, set the Type
property to Integer.
Set the MaximumValue property to 150 and the MinimumValue property to 0.
6|Page
The Regular Expression Editor dialog box contains a list of standard regular expression patterns
that you can choose from. Alternatively, you can type a custom regular expression pattern into the
Validation Expression text box. Scroll down and select the “U.S. Social Security number” option.
Click the OK button to set the RegularExpressionValidator’s ValidationExpression to this value.
7|Page