Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start Date - End Date validation using Compare Validator Misfires

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.

like image 786
3lokh Avatar asked May 14 '26 20:05

3lokh


2 Answers

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>
like image 104
shanish Avatar answered May 17 '26 09:05

shanish


This one is correct.. It solved my problem.

<asp:CompareValidator ID="cmpVal1" ControlToCompare="txtStartDate" ControlToValidate="txtEndDate" Type="Date" Operator="GreaterThanEqual"  ErrorMessage="ToDate should be greater than FromDate" runat="server"></asp:CompareValidator>

And don't forget to write:

cmpVal1.Validate() 

on the event where comparison occurs.

like image 32
Rohini Avatar answered May 17 '26 08:05

Rohini