Introduction to Validation
The Web Forms framework includes a set of validation server controls that provide an easy-to-use but powerful way
to check input forms for errors and, if necessary, display messages to the user.
Validation controls are added to a Web Forms page like other server controls. There are controls for specific types of
validation, such as range checking or pattern matching, plus a RequiredFieldValidator that ensures that a user does
not skip an entry field. You can attach more than one validation control to an input control. For example, you
might specify both that an entry is required and that it must contain a specific range of values.
Validation controls work with a limited subset of HTML and Web server controls. For each control, a specific
property contains the value to be validated. The following table lists the input controls that may be validated.
| Control |
Validation Property |
| HtmlInputText |
Value |
| HtmlTextArea |
Value |
| HtmlSelect |
Value |
| HtmlInputFile |
Value |
| TextBox |
Text |
| ListBox |
SelectedItem.Value |
| DropDownList |
SelectedItem.Value |
| RadioButtonList |
SelectedItem.Value |
Types of Validation Controls
The simplest form of validation is a required field. If the user enters any value in a field, it is valid.
If all of the fields in the page are valid, the page is valid. The following example illustrates this using the RequiredFieldValidator.
There are also validation controls for specific types of validation, such as range checking or pattern matching. The following table lists the validation controls.
| Control Name |
Description |
| RequiredFieldValidator |
Ensures that the user does not skip an entry. |
| CompareValidator |
Compares a user's entry with a constant value or a property value of another control using a comparison operator (less than, equal to, greater than, and so on). |
| RangeValidator |
Checks that a user's entry is between specified lower and upper boundaries. You can check ranges within pairs of numbers, alphabetic characters, or dates. Boundaries can be expressed as constants. |
| RegularExpressionValidator |
Checks that the entry matches a pattern defined by a regular expression. This type of validation allows you to check for predictable sequences of characters, such as those in social security numbers, e-mail addresses, telephone numbers, postal codes, and so on. |
| CustomValidator |
Checks the user's entry using validation logic that you code yourself. This type of validation allows you to check for values derived at run time. |
| ValidationSummary |
Displays the validation errors in summary form for all of the validators on a page. |