To better create interactive Web applications, to enhance application security (for example, to prevent scripting intrusions, etc.), developers should provide validation capabilities for the parts of the user input. In the past, the input validation function is basically written by the client script to complete this implementation is cumbersome and prone to errors. With the development of technology, ASP. NET technology overcomes these drawbacks by providing a series of validation controls, such as RequiredFieldValidator, CompareValidator, RangeValidator, and so on. Using these validation controls, developers can add input validation to Web pages, such as defining validation rules, defining error message content to be displayed to the user, and so on. Typically, ASP. NET provides validation controls that meet the needs of most Web applications, however, in some cases, built-in validation controls cannot fulfill the specific requirements of application requirements for data entry. To remedy this shortcoming, ASP.net 2.0 defines an extensible validation framework that can be used in control development. Developers can define validation controls themselves by using this validation framework. This article introduces important aspects of implementing validation controls, including a built-in validation control overview, validation frameworks, and so on.
1. Built-in validation Controls Overview
The validation control completes the ability to validate the input data. By associating a validation control with an input control, developers can add input validation to a Web page. In addition, the methods for customizing validation rules and the content and display of custom error messages are provided. This section provides an overview of the ASP.net built-in validation controls.
ASP.net 2.0 contains 5 built-in validation controls: RequiredFieldValidator, CompareValidator, RangeValidator, RegularExpressionValidator and CustomValidator, these controls derive directly or indirectly from System.Web.UI.WebControls.BaseValidator. Each validation control performs a specific type of validation and displays a custom message when validation fails. The 5 validation controls are briefly described below.
(1) RequiredFieldValidator Control
The control is used to ensure that a value is included in the control being validated.
(2) CompareValidator control
The control uses a comparison operator (less than, equal to, greater than, and so on) to compare user input to a constant value or to a property value of another control.
(3) RangeValidator control
This control is used to check whether the user's input is within a specified upper or lower limit. You can check the range of numeric pairs, alphabetic character pairs, and date pairs.
(4) RegularExpressionValidator control
This control is used to check whether the item matches the pattern defined by the regular expression. This type of validation allows you to check for predictable sequences of characters, such as ID numbers, e-mail addresses, phone numbers, postal codes, and so on.
(5) CustomValidator control
This control is used to check user input using validation logic that you write yourself. This type of validation allows you to check the values that are exported at run time.
In addition to the built-in validation controls above, ASP.net 2.0 provides a control validationsummary for displaying an error message profile. The purpose of the control is to display the error messages from all validation controls on the page together in one location, such as a message box or a list of error messages. The ValidationSummary control does not perform validation, but it can be used with all validation controls, and more accurately, ValidationSummary can work with the 5 built-in validation controls as well as the custom validation controls to complete the validation function.
To use validation controls on a Web page, we need to be aware of several key aspects of the following.
First, you associate the validation control with the input control, and then define the related properties based on the characteristics of the different types of validation controls. For example, all validation controls have to be associated with the Contrltovalidate property to define the error message content through the ErrorMessage property, and for the scope check control RangeValidator, You must define the MaximumValue and MinimumValue properties to specify the minimum and maximum values for a valid range, and for a pattern-matching control RegularExpressionValidator, You must use the ValidationExpression property to specify the regular expression used to validate the input control. The use of the above description is likely to allow an input control to associate multiple validation controls, which is allowed in ASP.net 2.0.