|
Validation Controls
Validation is the process of making sure that the user enters correct information
into a form. Validation controls provided by .NET Framework work in the client
browser if the browser supports Javascript and DHTML and checks the data the user
entered before sending it to the server. All the validation takes place in the browser
and nothing is sent back to the server. If the browser doesn't support DHTML and scripting
then validation is done on the server. All validation controls in the .NET Framework
are derived from the BaseValidator class. This class serves
as the base abstract class for the validation controls and provides the implementation
for all validation controls that derive from this class.
The validation controls that are provided by the .NET Framework are as follows:
RequiredFieldValidator
CompareValidator
RangeValidator
RegularExpressionValidator
CustomValidator
Common to all the above said controls are the ErrorMessage and ControlToValidate properties.
The ErrorMessage property is used to set an error to be displayed and the ControlToValidate
property is used to set the control you want to check.
RequiredFieldValidator
Simplest of all, the RequiredField validator makes sure that the user enters
data into a form. For example, on a registration form you might want your users
to enter their date of birth in a textbox. If they leave this field empty,
this validation control will display an error. The class hierarchy for this control
is as follows:
Object
Control
WebControl
Label
BaseValidator
RequiredFieldValidator
Notable property of the RequiredFieldValidator is the InitialValue property
which sets an initial value in the control.
CompareValidator
Comparison validators are used to compare the values entered by the user into a control
(textbox) with the value entered into another control or with a constant value. We
indicate the control to validate by setting the ControlToValidate property
and if we want to compare a specific control with another control we need to set the ControlToCompare property
to specify the control to compare with. The class hierarchy for this control is as
follows:
Object
Control
WebControl
Label
BaseValidator
BaseCompareValidator
CompareValidator
RangeValidator
Range Validators are used to test if the value of a control is inside a specified
range of values. The three main properties of this control are the ControlToValidate property
which contains the control to validate and Maximum and Minimum values
which hold the maximum and minimum values of the valid range. The class hierarchy
for this control is as follows:
Object
Control
WebControl
Label
BaseValidator
BaseCompareValidator
RangeValidator
RegularExpressionValidator
RegularExpression validators are used to check if the value in a control matches a
pattern defined by the regular expression. Notable property for this control
is the ValidationExpression property which allows us
to select a predefined expression which we want to match with the data entered in
a control. The class hierarchy for this control is as follows:
Object
Control
WebControl
Label
BaseValidator
RegularExpressionValidator
CustomValidator
Custom validators are used to perform our own validation for the data in a control.
For example, you can check the value entered by a user is even or odd
with this control which is not possible with any of the above mentioned validation
controls. You write the script for the validation using Javascript or VBScript
and associate that script function to the ClientValidationFunction property
of this control. The class hierarchy for this control is as follows:
Object
Control
WebControl
Label
BaseValidator
CustomValidator
ValidationSummary
Validation summary control is used to display a summary of all validation errors (from
all validation controls) on a Web page. This class is supported by the ValidationSummary class
and the hierarchy is as follows:
Object
Control
WebControl
ValidationSummary
Notable property of this control is the DisplayMode property
which allows us to display the errors in a specific format, example, as a list, paragraph or
as a bulletlist.
Sample Application>>
|