CompareValidator Server Control in ASP.Net

The CompareValidator control enables you to compare two form elements as well as to compare values contained within form elements to constants that you specify. For example, you can specify that a form element’s value must be an integer and greater than a specified number. You can also state that values must be strings, dates, or other data types that are at your disposal.
Validating Against Other Controls
One of the more common ways of using the CompareValidator control is to make a comparison between two form elements. For example, suppose that you have an application that requires users to have passwords in order to access the site. You create one text box asking for the user’s password and a second text box that asks the user to confirm the password. Because the text box is in password mode, the end user cannot see what she is typing — just the number of characters that she has typed. To reduce the chances of the end user mistyping her password and inputting this incorrect password into the system, you ask her to confirm the password. After the form is input into the system, you simply have to make a comparison between the two text boxes to see whether they match. If they match, it is likely that the end user typed the password correctly, and you can input the password choice into the system. If the two text boxes do not match, you want the form to be invalid.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>CompareFieldValidator</title>
</head>
<body>
    <form runat="server" id="Form1">
        <p>
            Password<br />
            <asp:textbox id="TextBox1" runat="server" textmode="Password"></asp:textbox>
            <asp:comparevalidator id="CompareValidator1" runat="server" text="Passwords do not match!" controltovalidate="TextBox2" controltocompare="TextBox1"></asp:comparevalidator>
        </p>
        <p>
            Confirm Password<br />
            <asp:textbox id="TextBox2" runat="server" textmode="Password"></asp:textbox>
        </p>
        <p>
            <asp:button id="Button1" onclick="Button1_Click" runat="server" text="Login"></asp:button>
        </p>
        <p>
            <asp:label id="Label1" runat="server"></asp:label>
        </p>
    </form>
</body>
</html>
<%@ Page Language="C#" %>
<script runat="server">
protected void Button1_Click(Object sender, EventArgs e) {
    if (Page.IsValid)
    Label1.Text = “Passwords match";
}
</script>

Looking at the CompareValidator control on the form, you can see that it is similar to the RequiredFieldValidator control. The CompareValidator control has a property called ControlToValidate that associates itself with one of the form elements on the page. In this case, you need only a single CompareValidator control on the page because a single comparison is made. In this example, you are making a comparison between the value of TextBox2 and that of TextBox1. Therefore, you use the ControlToCompare property. This specifies what value is compared to TextBox2. In this case, the value is TextBox1.
Validating Against Constants
Besides being able to validate values against values in other controls, you can also use the CompareValidator control to make comparisons against constants of specific data types. For example, suppose you have a text box on your registration form that asks for the age of the user. In most cases, you want to get back an actual number and not something such as aa or bb as a value. Listing 4-9 shows you how to ensure that you get back an actual number.

Age : <asp:TextBox ID="TextBox1" runat="server" MaxLength="3"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" Text="You must enter a number" ControlToValidate="TextBox1" Type="Integer" Operator="DataTypeCheck"></asp:CompareValidator>

In this example, the end user is required to type a number into the text box. If she attempts to bypass the validation by entering a fake value that contains anything other than a number, the page is identified as invalid, and the CompareValidator control displays the value of the Text property.
To specify the data types that you want to use in these comparisons, you simply use the Type property. The type property can take the following values:

  • Currency
  • Date
  • Double
  • Integer
  • String

Not only can you make sure that what is entered is of a specific data type, but you can also make sure that what is entered is valid when compared to specific constants. For example, you can make sure what is entered in a form element is greater than, less than, equal to, greater than or equal to, or less than or equal to a specified value.

Age : <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" Operator="GreaterThan" ValueToCompare="18" ControlToValidate="TextBox1" Text="You must be older than 18 to join" Type="Integer"></asp:CompareValidator>

In this case, the CompareValidator control not only associates itself with the TextBox1 control and requires that the value must be an integer, but it also uses the Operator and the ValueToCompare properties to ensure that the number is greater than 18. Therefore, if the end user enters a value of 18 or less, the validation fails, and the page is considered invalid.
The Operator property can take one of the following values:

  • Equal
  • NotEqual
  • GreaterThan
  • GreaterThanEqual
  • LessThan
  • LessThanEqual
  • DataTypeCheck

The ValueToCompare property is where you place the constant value used in the comparison. In the  preceding example, it is the number 18.

Tagged . Bookmark the permalink.

Leave a Reply