Link to home
Start Free TrialLog in
Avatar of crescendo
crescendo

asked on

Validating dates in format dd MMM yyyy

I am writing an app that will be accessed by UK and US users, so date input is tricky. I decided to enforce date input in the format dd MMM yyyy (eg 05 Oct 2004) as it is unambiguous. I used a regular expression validator to check for 2 digits, a space, 3 alpha, a space, 4 digits. That bit works. But when I use a RangeValidator to check it is a valid date, so that people don't input 99 XYZ 1234, it fails even on 'valid' dates.

Any suggestions how I can handle UK and US date input?
Avatar of crescendo
crescendo

ASKER

Sorry, I meant a CompareValidator, not a RangeValidator.
Can u add the regular expression for clarity?
Maybe a globalization settings issue?

If all else fails, I suppose you can use two Regex validators. Inelegant, perhaps, but it should work.
I think I wasn't clear.

The RegEx works, so I can validate the general format. BUT... I still need to check that the month names and number of days in the month are valid, e.g. not allow 29 Feb 2003. I hoped the CompareValidator, set to check a date data type, would do this, but it complains because it presumably expects 30/11/2004 (UK) format dates, not 30 Nov 2004.

I can't change the regional settings because there are other webs on the server and it might affect them.
Why dont you try writing a java script for the same to validate the dates?
<<Why dont you try writing a java script for the same to validate the dates?>>

I was assuming that I wasn't the first person in the universe to have to accept and validate dates from both UK and US users, so there must be a solution out there which would save me doing all the work myself.

I'll write it in server-side code.

Thanks

Just a suggestion: Why dont you use a dropdown for the months to enforce MMM or may be months and years?
I'd still have the same problem of validating the number of days in each month.

I'm just surprised that the CompareValidator doesn't allow this format. Perhaps it only allows the format set on the regional settings, so if I set that it would work. But, as I said above, that would break other apps.
I believe you can handle the System.Globalization issues on a more local level. This may help, may not. I haven't really had to deal with this issue before.

DateTimeFormatInfo Class
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemglobalizationdatetimeformatinfoclasstopic.asp

Once America takes over THE REST of the world, this won't be so much of an issue. Metric system be damned.

I wouldn't be surprised if you can tweak the culture settings for the validator.
ASKER CERTIFIED SOLUTION
Avatar of dabitbol
dabitbol

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
glad I could help!