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"



shmzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gfuture4meCommented:
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>
nmwis70Commented:
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.
DevastatedCommented:
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.
DevastatedCommented:
The two dates you supplied work as expected with the above code by the way.
scaffmasterCommented:
By default your .net app will use the input locale of your computer, check you have the correct locale set e.g. Uk for a dd/mm/yyyy date format over a US mm/dd/yyyy format.

You can also edit the locale for the specific app in the web.config file by setting the culture as

<globalization
    requestEncoding="utf-8"
    responseEncoding="utf-8"
    culture="en-GB"
    uiCulture="en-GB" />

I only ever deal with UK for dates etc. so sorry if my assumptions are incorrect.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.