How to use CompareValidator control in

CompareValidator control compare user inputted value with the another input control value or a constant value. comparevalidator validation success if inputted value matches the criteria specified by the Operator, ValueToCompare or ControlToCompare property. comparevalidator Type property indicate whether inputted value need to converted to specified data type (string, date, currency, double or integer). ControlToValidate property indicate which control need to validate. ControlToCompare property indicate which control need to compare with inputted value (such as TextBox value). ValueToCompare property indicate that you want to compare inputted value with a constant value instead other control value. Don't put ControlToValidate and ValueToCompare properties together in a comparevalidator control. you can use any one property at a time. that means you can only compare with a control or compare with a constant value at the same time.

you can use the Operator property to specify the type of comparison to perform, such as equal to, greater than, equal, not equal, less than, less than equal, data type check etc. Operator property value DataTypeCheck validated the data type specified by the Type property. so the comparevalidator validation fails if the inputted value cannot be converted to the specified data type.

this example demonstrate you how can we compare (validate) to password field (textbox) using comparevalidator control. if user inputted password not matches in both textboxes then validation fails and browser show a validation error message. if both textbox text matches then validation success.

<%@ Page Language="C#" %>
<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender,System.EventArgs e){
        Label1.Text = "Form Submited and Password match.";

<html xmlns="">
    <head id="Head1" runat="server">
        <title>CompareValidator example: how to use CompareValidator control in</title>
        <form id="form1" runat="server">
                <asp:Label ID="Label1" runat="server"></asp:Label>
                <br />

                <asp:Label ID="Label2" runat="server" Text="<u>P</u>assword" AccessKey="P" AssociatedControlID="TextBox1"></asp:Label>
                <asp:TextBox ID="TextBox1" runat="server" TextMode="Password"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" Text="*"></asp:RequiredFieldValidator>
                <asp:CompareValidator ID="CompareValidator" runat="server" ControlToValidate="TextBox1" ControlToCompare="TextBox2" ErrorMessage="Password does not match!">
                <br />

                <asp:Label ID="Label3" runat="server" Text="Re-Type Password" AssociatedControlID="TextBox2"></asp:Label>
                <asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
                <br />
                <asp:Button ID="Button1" runat="server" Text="Compare" OnClick="Button1_Click" />
More example