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

Validating User Input With Validation Controls

1. The document discusses different types of input validation controls in ASP.NET including required field, data type, range, comparison, and pattern validation. 2. It provides examples of how to use validation controls like RequiredFieldValidator, CompareValidator, and RangeValidator to validate user input fields on a form. 3. The form collects name, age, social security number, number of children, and number of male children, and different validation controls are demonstrated for each field.

Uploaded by

B man
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Validating User Input With Validation Controls

1. The document discusses different types of input validation controls in ASP.NET including required field, data type, range, comparison, and pattern validation. 2. It provides examples of how to use validation controls like RequiredFieldValidator, CompareValidator, and RangeValidator to validate user input fields on a form. 3. The form collects name, age, social security number, number of children, and number of male children, and different validation controls are demonstrated for each field.

Uploaded by

B man
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Validating User Input with Validation

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.

Types of Input Validation


Input validation can be broken into five distinct classes.
1. Required Field Input Validation
Required field validation is used to ensure that a value has been entered for a particular form
field.
2. Data Type Validation
Data type validation helps ensure that the text entered by the user can be converted into the data
format needed by the code. When users are prompted for the year they were born, it is important
that they enter the year as four digits, such as 1978, rather than as a string, such as Nineteen
seventy eight.
3. Range Input Validation
For certain numeric inputs, it is important that the value falls within a certain range of numbers.
When prompting a user for her age, we might want to ensure that the value entered is between 0
and 150.
4. Comparison Validation
Consider a web page that asks visitors to enter their annual income. For the input to be valid, the
income amount needs to be a numeric value greater than or equal to 0. Alternatively, we may
need to compare the value of one user input with the value of another. Imagine that in addition to
their income, users are asked to provide their income tax burden. Because a person’s income tax
cannot exceed total income, we need to ensure that the value entered for the income tax is less
than the value entered for the income.
5. Pattern Validation

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.

Validating User Input in an ASP.NET Page

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.

An ASP.NET Page for Examining the 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

Examining the RequiredFieldValidator Validation Control


User input can be divided into two categories: required input and optional input. Required input
is the set of input that the user must provide, whereas optional input is just that—optional. To
ensure that the user provides a value for a particular required input, use the
RequiredFieldValidator Web control.
 Add a RequiredFieldValidator validation Web control to the
ValidationControlTestBed.aspx page by dragging it from the Toolbox onto the page,
placing it immediately to the right of the Name TextBox.

Specifying What Web Control the Validation Web Control


Is Validating
The validation Web controls are designed to validate input for a particular input Web control. An
input Web control is one that is used to collect user input, such as the TextBox Web control.

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.

Examining the CompareValidator


The CompareValidator validation control is useful for comparing the value of a user’s input to a
constant value or to the value of a different user input. For example, the last two inputs on the
ValidationControlTestBed.aspx page ask users for the total number of children they have and the
number of male children.
 Add a CompareValidator to the page, placing it to the right of the TotalChildren
TextBox.
The CompareValidator is capable of performing a number of types of comparisons. For instance,
the CompareValidator can compare an input to ensure that it’s less than some value, greater than
or equal to a value, or not equal to some value.
The Operator property specifies what comparison the CompareValidator should perform.
 Select the CompareValidator so that its properties are loaded in the Properties window
and then scroll down to the Operator property.
The Operator property can be set to one of the following comparisons:

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.

Using the CompareValidator to Compare One Input to Another

 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.

Using the RangeValidator

 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.

Validating Input with the RegularExpressionValidator


Many forms of user input must be entered in a certain format. For example, an email address
must follow this particular format: one to many alphanumeric characters; the at symbol (@); one
to many alphanumeric characters; and concluding with a period (.) followed by a top-level
domain name, such as com, net, org, edu, us, uk, fr, and so on.
To ensure that a string input meets some specified format, we can use a
RegularExpressionValidator. The RegularExpressionValidator uses regular expressions to
determine whether the user’s input matches the accepted pattern. A regular expression is a string
that contains characters and special symbols and specifies a general pattern. Fortunately, you do
not need to be well versed in regular expression syntax to be able to use the
RegularExpressionValidator.
To ensure that the Social Security number is inputted in a proper format, add a
RegularExpressionValidator Web control to the ASP.NET page. Place it to the right of the SSN
TextBox Web control.
Set the RegularExpressionValidator’s ControlToValidate property to SSN and its ErrorMessage
property to Your Social Security number must be in the format XXX-XX-XXXX. After you set
these two properties, the only other property you need to configure is the ValidationExpression
property, which specifies the regular expression pattern that the user’s input must conform to.
To edit this property, click the ValidationExpression property; to the right you will see an
ellipsis. Clicking the ellipsis displays the Regular Expression Editor dialog box.

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

You might also like