Programming Microsoft ASP.NET 4 - Dino Esposito [201]
Note
Typical control members involved with input validation have been grouped in the IValidator interface that the BaseValidator class implements. The interface includes the Validate method and the IsValid and ErrorMessage properties.
Generalities of Validation Controls
Each validation control references an input control located elsewhere on the page. When the page is submitted, the content of the monitored server control is passed to the associated validation control for further processing. Each validation control performs a different type of verification. Table 9-3 shows the types of validation supported by the .NET Framework.
Table 9-3. Validation Controls in ASP.NET
Validation Control
Description
CompareValidator
Compares the user’s entry against a fixed value by using a comparison operator such as LessThan, Equal, or GreaterThan. It can also compare against the value of a property in another control on the same page.
CustomValidator
Employs programmatically defined validation logic to check the validity of the user’s entry. You use this validator when the other validators cannot perform the necessary validation and you want to provide custom code that validates the input.
RangeValidator
Ensures that the user’s entry falls within a specified range. Lower and upper boundaries can be expressed as numbers, strings, or dates.
RegularExpressionValidator
Validates the user’s entry only if it matches a pattern defined by a regular expression.
RequiredFieldValidator
Ensures that the user specifies a value for the field.
Multiple validation controls can be used with an individual input control to validate according to different criteria. For example, you can apply multiple validation controls on a text box that is expected to contain an e-mail address. In particular, you can impose that the field is not skipped (RequiredFieldValidator) and that its content matches the typical format of e-mail addresses (RegularExpressionValidator).
Table 9-3 lacks a reference to the ValidationSummary control. The control does not perform validation tasks itself. Instead, it displays a label to summarize all the validation error messages found on a Web page as the effect of other validators. I’ll cover the ValidationSummary control later in the chapter.
The BaseValidator Class
Table 9-4 details the specific properties of validation controls. Some properties—such as ForeColor, Enabled, and Text—are overridden versions of base properties on base classes.
Table 9-4. Basic Properties of Validators
Property
Description
ControlToValidate
Gets or sets the input control to validate. The control is identified by name—that is, by using the value of the ID attribute.
Display
If client-side validation is supported and enabled, gets or sets how the space for the error message should be allocated—either statically or dynamically. In the case of server-side validation, this property is ignored. A Static display is possible only if the browser supports the display CSS style. The default is Dynamic.
EnableClientScript
True by default; gets or sets whether client-side validation is enabled.
Enabled
Gets or sets whether the validation control is enabled.
ErrorMessage
Gets or sets the text for the error message.
ForeColor
Gets or sets the color of the message displayed when validation fails.
IsValid
Gets or sets whether the associated input control passes validation.
SetFocusOnError
Indicates whether the focus is moved to the control where validation