Validation Server Control
 
Working of ASP.NET Validation Server Controls
 
RequiredFieldValidator Server Control
 
The RequiredFieldValidator control ensures that the user does not skip an entry. The control fails validation if the value it contains does not change from its initial value when validation is performed. If all the fields in the page are valid, the page is valid.
 
<asp:RequiredFieldValidator ID= “RequiredFieldValidator1? Runat= “server?
ErrorMessage= “Field is invalid? ControlToValidate= “Textbox1? >
</asp:RequiredFieldValidator>
 
RangeValidator Server Control
 
The RangeValidator control checks for an input value falls within a given range. RangeValidator uses three properties to perform its validation:
 
1. ControlToValidate contains the value to validate

2. MinimumValue defines the minimum value of the valid range

3. MaximumValue defines the maximum value of the valid range. These constants are stored as string values, but are converted to the data type defined by Type when the comparison is performed.
 
Sample code to check user’s input between two values.
 
<asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage="Value
must be between 20 and 30" ControlToValidate="Textbox1" Type="Integer"
MinimumValue="20" MaximumValue="30" Text="InValid Range">
</asp:RangeValidator>
<asp:TextBox ID="RangeTextbox" runat="server"></asp:TextBox>
 
RegularExpressionValidator Server Control
 
The RegularExpressionValidator control ensures that the entered value matches a pattern defined by a regular expression. This type of validation allows you to check for predictable sequences of characters, such as those in social security numbers, e-mail addresses, telephone numbers, postal codes, and so on.
 
RegularExpressionValidator uses two properties to perform its validation:
 
1. ControlToValidate contains the value to validate

2. ValidationExpression contains the regular expression to match.
 
Here is a sample code to validate the e-mail format.
 
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" Text="*" ControlToValidate="Textbox4" runat="server"
 
ErrorMessage="You must enter an email address" ValidationGroup="CheckMail"
 
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"> </asp:RegularExpressionValidator>
 
Note: You can get the list of predefined ValidationExpression from properties window on selecting the RegularExpressionValidator.
 
CompareValidator Server Control
 
The CompareValidator control compares the value of one control to another, or to an explicit value in the control's ValueToCompare property.
 
CompareValidator has three properties to perform its validation.
 
1. ControlToValidate

2. ControlToCompare contain the values to compare.

3. Operator defines the type of comparison to perform and operator property can be one of the following values

• Equal

• NotEqual

• GreaterThan

• GreaterThanEqual

• LessThan

• LessThanEqual

• DataTypeCheck
 
CompareValidator performs the validation by evaluating these properties as an expression, as shown in the following example.
 
<ControlToValidate> <Operator> <ControlToCompare>
 
Sample code to compare value of TextBox3 with TextBox2
 
<asp:CompareValidator ID="CompareValidator1" runat="server"
 
ErrorMessage="Passwords don't match!" ControlToValidate="TextBox3"
 
ControlToCompare="Textbox2" ></asp:CompareValidator>
 
If the expression evaluates true, the validation result is valid.
 
CustomValidator Server Control
 
Some times the given validation controls (other than Customvalidator) are unable to fulfill the requirement for a web page then CustomValidator is used. As validation can be done on the client side or on the server side, depending on the browser, the CustomValidator has attributes for applying a server-side and a client-side method for validation.
 
The CustomValidator control calls a user-defined function to perform validations that the standard validators can't handle. The custom function can execute on the server or in client-side script.
 
CustomValidator using Client Side custom Validation
 
For Client Side Validation using CustomValidator, client side method must be written in scripting language known to browser such as JavaScript or VBScript.
 
To make client-side custom validation set the ClientValidationFunction property. The name of the custom function must be identified in the ClientValidationFunction property.
 
The custom function must have the form Sample code for client side script for function
 
<script type="text/javascript">
function validatenumber(oSrc,args)
{
args.IsValid = (args.Value % 6 == 0)
}
</script>

<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="Textbox1" Text="*" ClientValidationFunction="validatenumber"
ErrorMessage="This Number is not divisible by 6">
</asp:CustomValidator>
 
Note that source is the client-side CustomValidator object, and an argument is an object with two properties, Value and IsValid. The Value property is the value to be validated and the IsValid property is a Boolean used to set the return result of the validation.
 
CustomValidator using server-side custom validation
 
For server-side custom validation, the ServerValidateEventArgs object is used as one of the parameters to get data passed to the function to check the validations.
 
To make server-side custom validation set the OnServerValidate property. The name of the custom function must be identified in the OnServerValidate property.
 
Sample code for server side script function.
 
<script language="C#" runat ="server">
void validatenumber(object source,ServerValidateEventArgs args)
{
args.IsValid = (int.Parse (args.Value.ToString ()) % 6 == 0);
}
</script>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="Textbox1" Text="*" OnServerValidate="validatenumber"
ErrorMessage=" This Number is not divisible by 6">
</asp:CustomValidator>
 
ValidationSummary Server Control
 
This control is not used for validations while it gives collective information about the other validation controls on a Web Forms if the validation conditions are not fulfilled. When the user's input is processed, the Web Forms framework passes the user's entry to the connected validation control or controls. The IsValid property on the page is set after processing all validation controls. The entire page is set to invalid, if any of the controls fails for validation check.
 
A ValidationSummary control is displayed, if the IsValid property of the page is set to false. The displaying errors with a ValidationSummary can have three format types.
 
1. BulletList

2. List

3. SingleParagraph

4. As Dialog Box