Compare Validatior Control in Asp.Net C#


The CompareValidator control compares a value in one control with a fixed value, or, a value in another control.
CompareValidator control compare user inputted value with the another input control value or a constant value. 


             Html Coding

<html xmlns="">
<head id="Head1" runat="server">
    <form id="form1" runat="server">

            <asp:Label ID="Label1" runat="server" Text="Password"></asp:Label>
                <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
                <asp:CompareValidator ID="CompareValidator1"
                    runat="server" ControlToCompare="txtConfirmPassword" ControlToValidate="txtPassword"></asp:CompareValidator>
            <asp:Label ID="Label2" runat="server" Text="Confirm Password"></asp:Label>
                <asp:TextBox ID="txtConfirmPassword" runat="server"></asp:TextBox>
                <asp:CompareValidator ID="CompareValidator2"
                     runat="server" ControlToCompare="txtPassword"
                    ControlToValidate="txtConfirmPassword" ErrorMessage="Password Not Same"></asp:CompareValidator>
        <tr><td colspan ="2">
            <asp:Button ID="Button1" runat="server" Text="Save" />


comparevalidator validation success if inputted value matches the criteria specified by the Operator, ValueToCompare or ControlToCompare property. 


Typeit specifies the data type
ControlToCompareit specifies the value of the input control to compare with
ValueToCompareit specifies the constant value to compare with
Operatorit specifies the comparison operator, the available values are: Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual and DataTypeCheck

First - Add New - Web Form - and Select Button & Labels & TextBoxs From ToolBox.

Select - Compare Validation Control  From Validation Tool

Next - Select Compare Validation  Click F4 Property Window  for Password  Control to Validate = txtPassword & Control to Compare =txtConfirmPassword  Error Message empty

Next - Select Compare Validation  Press - F4 Property Window  for Confirm Password Control to Validate =txtConfirmPassword  & Control to Compare =txtPassword  Error Message Given to Required

Html Code For Compare Validation 

          Next - F5 Click - Run the Form  -   Button  Enter the Password Incorrect    Show the  Error Message    below like that


        Now  Enter the Correct Password  Hide the Error Message 


Post a Comment