CompareValidator in ASP.NET

CompareValidator control is used to perform three different types of validation tasks.

  • To perform a data type check.
  • To compare the value entered into a form field against a given fixed value.
  • To compare the value of one form field against another field.

Important Properties

ValueToCompare: The fixed value against which to compare.

ControlToCompare: The ID of a control against which to compare.

Type: The type of value for comparison. Given values are String, Integer, Double, Date, and Currency.

Operator: Given values are DataTypeCheck, Equal, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual and NotEqual.

if you don’t enter a value into the field being validated, then CompareValidator will not display an error.

Example

<asp:TextBox D="txtConPassword"
        runat="server"
        Width="164px"
        TextMode="Password">
</asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
        ForeColor="Red"  
        ControlToCompare="txtPassword"        
        ControlToValidate="txtConPassword"
        ErrorMessage="Re-Enter Password">
</asp:CompareValidator>