The CompareValidator Server Control in asp.net


The CompareValidator Server Control:

The CompareValidator control allows you to make comparisons between two form elements as well as to compare values contained within form elements to constants that you specify. For instance, 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.

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 which 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 which 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 if 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.

Using the CompareValidator to test values against other control values

<%@ Page Language="C#" %>
<script runat="server">
    protected void Button1_Click(Object sender, EventArgs e) {
       Label1.Text = "Passwords match";
    }
    </script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CompareFieldValidator</title>
</head>
<body>
    <form runat="server">
        <p>
            Password<br>
            <asp:TextBox ID="TextBox1" Runat="server"
             TextMode="Password"></asp:TextBox>
             &nbsp;
            <asp:CompareValidator ID="CompareValidator1"
             Runat="server" ErrorMessage="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>

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.

 

Using the CompareValidator to validate against constants

Age:

<asp:TextBox ID=”TextBox1″ Runat=”server” MaxLength=”3″>

</asp:TextBox>

&nbsp;

<asp:CompareValidator ID=”CompareValidator1″ Runat=”server”

  ErrorMessage=”You must enter a number”

  ControlToValidate=”TextBox1″ Type=”Integer”

  Operator=”DataTypeCheck”></asp:CompareValidator>

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 instance, 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.

 Making comparisons with the CompareValidator control

Age:

<asp:TextBox ID=”TextBox1″ Runat=”server”></asp:TextBox>

  &nbsp;

<asp:CompareValidator ID=”CompareValidator1″ Runat=”server”

  Operator=”GreaterThan” ValueToCompare=”18″

  ControlToValidate=”TextBox1″

  ErrorMessage=”You must be older than 18 to join” Type=”Integer”>

</asp:CompareValidator>

The Operator property can take one of the following values:

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

Other Related Posts:

Validations:

ASP.NET Validation Server Controls

The RequiredFieldValidator Server Control in asp.net

Validating Drop-Down Lists with the RequiredFieldValidator Control

The CompareValidator Server Control in asp.net

The RangeValidator Server Control in asp.net

The RegularExpressionValidator Server Control in asp.net

The CustomValidator Server Control in asp.net

The ValidationSummary Server Control in asp.net

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s