How to use compare validator to compare the data between two dropdownlists values?

2.8k views Asked by At

I have two DropDownLists populated with Year-Dates, and I want to show an error message in the case where the second ddls value is less than the first ddls value.

This is the code I have used so far, and it doesn't work:

  <asp:CompareValidator 
       ID="cvEndYear2" Operator="GreaterThan" runat="server" CssClass="text-danger" 
       ValidationGroup="Save" ControlToValidate="ddlEndYear" Display="Dynamic" 
       ValueToCompare="ddlStartYear" ErrorMessage="Greater Than" SetFocusOnError="true">
  </asp:CompareValidator>
2

There are 2 answers

3
Tim Schmelter On BEST ANSWER

You have to specify the ControlToCompare and the Operator:

<asp:CompareValidator 
   ID="cvEndYear2" Operator="GreaterThan" runat="server" CssClass="text-danger" 
   ValidationGroup="Save" 
   ControlToValidate="ddlEndYear" Display="Dynamic" 
   ControlToCompare="ddlStartYear"
   Operator="GreaterThanEqual"
   Type="Integer"
   ErrorMessage="The end year must be greater/equal the start year" SetFocusOnError="true">
</asp:CompareValidator>
1
Ehsan Sajjad On

You haven't specified the type, specify the type of data which would be in the textboxes :

 <asp:CompareValidator 
       ..........
       ..........
       ControlToValidate="ddlStartYear" 
       ControlToCompare="ddlEndYear"
       Operator="GreaterThanEqual"
       Type="Integer">
  </asp:CompareValidator>

You might want to have a look at this tutorial