Link to home
Start Free TrialLog in
Avatar of Sh M
Sh MFlag for United States of America

asked on

ASp.NET date validation

Hi,
I am using the following to validate dates:


                                  <asp:CompareValidator id="valCompareDate"
                                          ControlToValidate="EndDate"
                                          ControlToCompare="StartDate"
                                          Operator="GreaterThan"
                                          Type="Date"
                                          Runat="Server">End date must be reater than start date</asp:CompareValidator>

date forma tis dd/mm/yyyy
But when I enter: startdate: 11/12/2004  and enddate: 23/02/2005 I get error message than end date should be greater....
It only happens when end date's day is twenty something.....

any idea?

thanks

ps: date format is:
ValidationExpression="(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d"



Avatar of gfuture4me
gfuture4me
Flag of United States of America image

This works out.....

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="TextBox1"
            ControlToValidate="TextBox2" ErrorMessage="CompareValidator" Operator="GreaterThan"
            Type="Date"></asp:CompareValidator>
Avatar of nmwis70
nmwis70

Looks like the problem is occuring because of a date format issue. I believe your code is confused expecting dates to be in the format of mm/dd/yyyy but instead you are passing it dates formatted dd/mm/yyyy (correct me if I am wrong).

One way to fix it is to require users to input dates as yyyy/mm/dd as this will compare correctly no matter what the current locale is, and you can do that by changing your validation expression to this so that it checks for that format.

Good luck.
Heya,

maybe it would be better to use the Control Validator with the ControlToValidate method... i.e. comparing the two dates that are entered in two seperate textboxes as follows...

Start Date: <asp:TextBox  id="txtStartDate"  Columns="10"  Runat="Server"/>
End Date: <asp:TextBox  id="txtEndDate"  Columns="10"  Runat="Server"/>

<asp:CompareValidator
 ControlToValidate="txtEndDate"
 ControlToCompare="txtStartDate"
 Display="Dynamic"
 Text="End date must be greater than start date!"
 Operator="GreaterThan"
 Type="Date"
 Runat="Server" />

hope this is at least food for thought.
The two dates you supplied work as expected with the above code by the way.
ASKER CERTIFIED SOLUTION
Avatar of scaffmaster
scaffmaster

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial