CompareValidator control in ASP.NET

CompareValidator validation control in ASP.NET is used to validate an input control’s value by comparing it with another control’s value or a constant value. For example, we can use CompareValidator to validate controls if we want user to enter same value in two different controls, to compare value of a control with another control, to compare value of a control against a constant value etc. We can also specify type of data to be accepted in a control using CompareValidator control.

Properties of CompareValidator control

Property Uses
ControlToValidate It takes ID of the control you want to validate
ControlToCompare It takes ID of the control against which you want to validate the control specified in the ControlToValidate property
Operator It specifies the type of comparison we want to use while comparing two controls. Possible values are “DataTypeCheck”, “Equal”, “GreaterThan”, “GreaterThanEqual”, “LessThan”, “LessThanEqual”, “NotEqual”
Type It is used to specify the data type of the control
ValueToCompare It is used to set a constant value against which the control specified in the ControlToValidate property is validated

Example 1: Validating a TextBox against another TextBox to match exact value

<asp:TextBox
     ID="txtPassword"
     runat="server"
     CssClass="textbox"
     TextMode="Password">
</asp:TextBox>

<asp:TextBox
     ID="txtConfirmPassword"
     runat="server"
     CssClass="textbox"
     TextMode="Password">
</asp:TextBox>

<asp:CompareValidator
     ID="CompareValidator1"
     runat="server"
     ControlToValidate="txtPassword"
     ControlToCompare="txtConfirmPassword"
     ErrorMessage="(Passwords do not match)"
     ForeColor="Red">
</asp:CompareValidator>

Here, we need to ensure that text of “txtPassword” must match the text of “txtConfirmPassword”. For this we have taken a CompareValidator and set its ControlToValidate property to “txtPassword” and ControlToCompare property to “txtConfirmPassword”

Example 2: Validating a TextBox against another TextBox using Operators

<asp:TextBox
     ID="txtDOB"
     runat="server"
     CssClass="textbox"
     Text="DD/MM/YYYY">
</asp:TextBox>

<asp:TextBox
     ID="txtDOJ"
     runat="server"
     CssClass="textbox"
     Text="DD/MM/YYYY">
</asp:TextBox>

<asp:CompareValidator
     ID="CompareValidator2"
     runat="server"
     ControlToValidate="txtDOB"
     ControlToCompare="txtDOJ"
     Operator="LessThan"
     Type=" Date"
     ErrorMessage="(DoB must be less than DoJ)"
     ForeColor="Red">
</asp:CompareValidator>

Here, we need to validate “txtDOB” and “txtDOJ” so that date of birth must be less than date of joining. For this validation, first we set ControlToValidate property to “txtDOB”, ControlToCompare property to “txtDOJ”. Then Operator property to “LessThan” and Type to “Double” which specifies that value of “txtDOB” and “txtDOJ” should be of Date type and value of “txtDOB” should be less than the value of “txtDOJ”

Example 3: Validating a TextBox against a constant value using Operators

<asp:TextBox
     ID="txtAge"
     runat="server"
     CssClass="textbox">
</asp:TextBox>

<asp:CompareValidator
     ID="CompareValidator3"
     runat="server"
     ControlToValidate="txtAge"
     ValueToCompare="18"
     Operator="GreaterThanEqual"
     Type="Integer"
     ErrorMessage="(Age cannot be less than 18)"
     ForeColor="Red">
</asp:CompareValidator>

<asp:Button
     ID="btnSubmit"
     runat="server"
     Text="Submit"
     CssClass="button"
     onclick="btnSubmit_Click" />

Here, we need to validate that the value of “txtAge” must be at least 18. For this we set ControlToValidate property to “txtAge”, ValueToCompare property to 18, Operator property to “GreaterThanEqual” and Type property to Integer

Complete HTML

<div style="width:400px">
     <div>
          <asp:Label
               ID="lblPassword"
               runat="server"
               Text="Password"
               AssociatedControlID="txtPassword">
          </asp:Label>
     </div>
     <div style="clear:both;"></div>
     <div>
          <asp:TextBox
               ID="txtPassword"
               runat="server"
               CssClass="textbox"
               TextMode="Password">
          </asp:TextBox>
     </div>
     <div style="clear:both;"></div>
     <div>
          <asp:Label
               ID="lblConfirmPassword"
               runat="server"
               Text="Confirm Password"
               AssociatedControlID="txtConfirmPassword">
          </asp:Label>
     </div>
     <div style="clear:both;"></div>
     <div>
          <asp:TextBox
               ID="txtConfirmPassword"
               runat="server"
               CssClass="textbox"
               TextMode="Password">
          </asp:TextBox>
          <asp:CompareValidator
               ID="CompareValidator1"
               runat="server"
               ControlToValidate="txtPassword"
               ControlToCompare="txtConfirmPassword"
               ErrorMessage="(Passwords do not match)"
               ForeColor="Red">
          </asp:CompareValidator>
     </div>
     <div style="clear:both;"></div>
     <div>
          <asp:Label
               ID="lblDOB"
               runat="server"
               Text="Date of Birth"
               AssociatedControlID="txtDOB">
          </asp:Label>
     </div>
     <div style="clear:both;"></div>
     <div>
          <asp:TextBox
               ID="txtDOB"
               runat="server"
               CssClass="textbox"
               Text="DD/MM/YYYY">
          </asp:TextBox>
     </div>
     <div style="clear:both;"></div>
     <div>
          <asp:Label
               ID="lblDOJ"
               runat="server"
               Text="Date of Birth"
               AssociatedControlID="txtDOJ">
          </asp:Label>
     </div>
     <div style="clear:both;"></div>
     <div>
          <asp:TextBox
               ID="txtDOJ"
               runat="server"
               CssClass="textbox"
               Text="DD/MM/YYYY">
          </asp:TextBox>
          <asp:CompareValidator
               ID="CompareValidator2"
               runat="server"
               ControlToValidate="txtDOB"
               ControlToCompare="txtDOJ"
               Operator="LessThan"
               Type="Date"
               ErrorMessage="(DoB must be less than DoJ)"
               ForeColor="Red">
          </asp:CompareValidator>
     </div>
     <div style="clear:both;"></div>
     <div>
          <asp:Label
               ID="lblAge"
               runat="server"
               Text="Age"
               AssociatedControlID="txtDOJ">
          </asp:Label>
     </div>
     <div style="clear:both;"></div>
     <div>
          <asp:TextBox
               ID="txtAge"
               runat="server"
               CssClass="textbox">
          </asp:TextBox>
          <asp:CompareValidator
               ID="CompareValidator3"
               runat="server"
               ControlToValidate="txtAge"
               ValueToCompare="18"
               Operator="GreaterThanEqual"
               Type="Integer"
               ErrorMessage="(Age cannot be less than 18)"
               ForeColor="Red">
          </asp:CompareValidator>
     </div>
     <div>
          <asp:Button
               ID="btnSubmit"
               runat="server"
               Text="Submit"
               CssClass="button"
               onclick="btnSubmit_Click" />
     </div>
</div>

Errors in following format is displayed if validations fails

CompareValidator control

Note: CompareValidator does not give error if controls are blank. So you need to add a RequiredFieldValidator control also to handle blank fields.

One thought on “CompareValidator control in ASP.NET

Leave a Reply

Your email address will not be published. Required fields are marked *