Validation Controls

A validation control enables us to validate an input and display an error message if necessary. It is very much like other server-side controls with certain additional methods and properties. First, the server treats it as an invisible control. After the user has entered erroneous data, it becomes visible. It is a powerful, rapid application development feature; however, a developer needs to understand its behavior and the methods thoroughly before he or she can appreciate it. There are certain rough edges in the Beta 2 version, which hopefully will be polished in the final product.The best strategy to learn the family of controls is to learn them one at a time, and finally to apply the summary validation.

Various types of validation controls are as follows:

■ RequiredFieldValidator Checks if the input control has any value.

■ RegularExpressionValidator Checks the value against a regular expression (pattern).

■ CompareValidator Checks if the value is acceptable compared to a given value or compared to the content of another control.

■ RangeValidator Checks if the input control's value is within a specified range.

■ CustomValidator Allows you to develop custom validation.

■ ValidationSummary Reports a summary of all errors.

By default, each of the validation controls performs the validation task at the client-side as well as at the server-side. Except for the RequiredFieldValidator, all other validation controls treat an empty field as a valid field. Therefore, we will need to apply a RequiredFieldValidator to every input field that we want to vali-date.You can attach more than one validation control to an input. For example, we may use a RequiredFieldValidator and a RangeValidator to ensure that an input is not empty and falls within a specified range.

There are a number of common properties in these controls. The major ones are:

■ ErrorMessage In case of an error, the system displays this message at the location of the control, and in the summary report, if any.

■ Display A validation control is kept invisible until a bad input is entered. In case of a bad input, the system has to display the error message. The display mechanism can be handled in one of three ways.

■ Display= "static" Initially, enough room in the page is reserved for the expected error message.

■ Display= "dynamic" No room is initially reserved. In case of an error, the message is displayed by displacing existing contents of the page.

■ Display="none" The message won't be displayed at the location of the control; however, it will be reported in the summary report, if any.

0 0

Post a comment