Validation Controls in ASP.NETSometimes you want to ensure that the all or some of data that user has entered in the form are valid and in proper format before submitting to the web server. To ensure that the all the data entered by the user is valid and in proper format, you need to check/validate the data before processing.
To make this easy for us Microsoft has introduced validation controls in ASP.NET that validates data both at the client side and at server side.
In ASP.NET, there are 6 validation controls
* RequiredFieldValidation Control
* CompareValidator Control
* RangeValidator Control
* RegularExpressionValidator Control
* CustomValidator Control
* Validation Summary
All of the validation controls inherit from the base class BaseValidator so they all have a series of properties and methods that are common to all validation controls. They are:
* ControlToValidate - This value is which control the validator is applied to.
* ErrorMessage - This is the error message that will be displayed in the validation summary.
* IsValid - Boolean value for whether or not the control is valid.
* Validate - Method to validate the input control and update the IsValid property.
* Display - This controls how the error message is shown. Here are the possible options:
o None (The validation message is never displayed.)
o Static (Space for the validation message is allocated in the page layout.)
o Dynamic (Space for the validation message is dynamically added to the page if validation fails.)
The RequiredFieldValidation Control
The first control we have is the RequiredFieldValidation Control. As it's obvious, it make sure that a user inputs a value. Here is how it's used:
Name ::
*
In this example, we have a textbox which will not be valid until the user types something in. Inside the validator tag, we have a single *. The text in the innerhtml will be shown in the controltovalidate if the control is not valid. It should be noted that the ErrorMessage attribute is not what is shown. The ErrorMessage tag is shown in the Validation Summary (see below).
The CompareValidator Control
Next we look at the CompareValidator Control. Usage of this CompareValidator is for confirming new passwords, checking if a departure date is before the arrival date, etc. We'll start of with a sample:
Textbox 1:
Textbox 2:
*
Here we have a sample where the two textboxes must be equal.
The tags that are unique to this control is the ControlToCompare attribute which is the control that will be compared. The two controls are compared with the type of comparison specified in the Operator attribute. The Operator attribute can contain Equal, GreterThan, LessThanOrEqual, etc.
The RangeValidator Control
Range validator control is another validator control which checks to see if a control value is within a valid range. The attributes that are necessary to this control are: MaximumValue, MinimumValue, and Type.
Sample:
Enter a date from between 2000 and 2009:
*
The RegularExpressionValidator Control
The regular expression validator is one of the more powerful features of ASP.NET. Everyone loves regular expressions. Especially when you write those really big nasty ones... and then a few days later, look at it and say to yourself. What does this do?
Again, the simple usage is
E-mail:
*
The CustomValidator Control
The final control we have included in ASP.NET is one that adds great flexibility to our validation abilities. We have a custom validator where we get to write out own functions and pass the control value to this function.
Field:
*
We notice that there are two new attributes ClientValidationFunction and OnServerValidate. These are the tell the validation control which functions to pass the controltovalidate value to. ClientValidationFunction is usually a javascript funtion included in the html to the user. OnServerValidate is the function that is server-side to check for validation if client does not support client-side validation.
Client Validation function:
Validation Summary
ASP.NET has provided an additional control that complements the validator controls. This is the validation summary control which is used like:
The validation summary control will collect all the error messages of all the non-valid controls and put them in a tidy list. The list can be either shown on the web page (as shown in the example above) or with a popup box (by specifying ShowMessageBox="True")
Thanks,