Tuesday, January 26, 2010

How many validation controls do you know?


With ASP.NET, there are six (6) controls included. They are:
1). RequiredFieldValidator
2). RangeValidator
3). CompareValidator
4). RegularExpressionValidator
5). CustomValidator
6). ValidationSummary
Note: - 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 controls 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 show. Here are the possible options:
·         None (The validation message is never displayed.)
·         Static (Space for the validation message is allocated in the page layout.)
·         Dynamic (Space for the validation message is dynamically added to the page if validation fails.)
1). RequiredFieldValidator Control
Enables you to require a user to enter a value in a form field.
Example: -
RequiredFieldValidation: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
            ControlToValidate="TextBox1" Display="Dynamic"
            ErrorMessage="* You must enter a value into textbox1"></asp:RequiredFieldValidator>
2). RangeValidator Control: -
Enables you to check whether a value falls between a certain minimum and maximum value. The attributes that are necessary to this control are: MaximumValue, MinimumValue, and Type.
Example: -
Enter a data from 1981:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server"
            ControlToValidate="TextBox1"
            ErrorMessage="* The date must be 12/15/1981 and 1/1/2010"
            MaximumValue="12/15/1981" MinimumValue="1/1/2010" Type="Date"></asp:RangeValidator>
3). CompareValidator Control: -
Enables you to compare a value against another value or perform a data type check.

Example: -
TextBox1: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
TextBox2: <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
            ErrorMessage="* You must enter the same values into both text box."
            ControlToCompare="TextBox2" ControlToValidate="TextBox1" Display="Dynamic"></asp:CompareValidator>
Note: - The data type can be one of: Currency, Double, Date, Integer or String. String being the default data type.
4). RegularExpressionValidator Control:
Enables you to compare a value against a regular expression.
Example: -
E-mail: - <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
            ControlToValidate="TextBox1" Display="Dynamic"
            ErrorMessage="* Please enter a valid e-mail address."
            ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
5). CustomValidator Control: -
Enables you to perform custom validation. Here we get to write out own functions and pass the control value to this function.
Example: -
Field:- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server"
            ClientValidationFunction="ClientValidate" OnServerValidate="ServerValidate" ControlToValidate="TextBox1"
            ErrorMessage="* This box is not valid." Display="Dynamic"></asp:CustomValidator>
Note: - There are two new attributes ClientValidationFunction and OnServerValidate. ClientValidationFunction is usually a javascript function  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 functions:
<script language="javascript">

/* Code goes here */

</script>
Server Validation functions:
public void ServerValidate(object obj, ServerValidateEventArgs args)
    {
        /*Code goes here */
    }
6). ValidationSummary Control: -
Enables you to display a summary of all validation error in a page.
Example: -
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
            DisplayMode="List" HeaderText="Errors:" ShowMessageBox="True" />
Note: - The validation summary control will collect all the error message of all the non – valid controls and put them in a tidy list. The list can be either show on the web page or with a popup box (by specifying ShowMessageBox = “True”)

No comments:

Post a Comment