Start Date - End Date validation using Compare Validator Misfires

3lokh picture 3lokh · Apr 16, 2012 · Viewed 43.3k times · Source

I need my end date to be always greater than start date, I tried validation using CompareValidator.

Code is as follows:

I have a text box start date

<asp:TextBox ID="TxtStartDate"
             runat="server" />

<asp:CalendarExtender Enabled="True"
                      ID="TxtStartDate_CalendarExtender"
                      TargetControlID="TxtStartDate"
                      runat="server" />

Another TextBox End date.

<asp:TextBox ID="TxtEndDate"
             runat="server" />

<asp:CalendarExtender Enabled="True"
                      ID="TxtEndDate_CalendarExtender"
                      TargetControlID="TxtEndDate"
                      runat="server" />

<asp:CompareValidator ControlToCompare="TxtStartDate"
                      ControlToValidate="TxtEndDate"
                      Display="Dynamic"
                      ErrorMessage="CompareValidator"
                      ID="CompareValidator1"
                      Operator="GreaterThan"
                      Type="Date"
                      runat="server" />

But the compare field validator misfires.

For example when start date is 2/04/2012 and end date is 10/04/2012 it fires.

Answer

shanish picture shanish · May 21, 2012

Simply you can try like this

<asp:CompareValidator ID="cmpVal1" ControlToCompare="txtStartDate" 
         ControlToValidate="txtEndDate" Type="Date" Operator="GreaterThanEqual"   
         ErrorMessage="*Invalid Data" runat="server"></asp:CompareValidator>