Sh M
asked on
ASp.NET date validation
Hi,
I am using the following to validate dates:
<asp:CompareValidator id="valCompareDate"
ControlToValidate="EndDate "
ControlToCompare="StartDat e"
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"
I am using the following to validate dates:
<asp:CompareValidator id="valCompareDate"
ControlToValidate="EndDate
ControlToCompare="StartDat
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
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.
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="txtEndD ate"
ControlToCompare="txtStart Date"
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.
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="txtEndD
ControlToCompare="txtStart
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<asp:TextBox ID="TextBox1" runat="server"></asp:TextB
<asp:TextBox ID="TextBox2" runat="server"></asp:TextB
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="TextBox1
ControlToValidate="TextBox
Type="Date"></asp:CompareV