validating date string

obscuregirl

Member
Joined
Sep 13, 2006
Messages
15
Location
UK
Programming Experience
Beginner
Hi

Bit of a newbie question I'm afraid.

My application currently uses datetimepicker controls, but because these can't show blank dates when no date is present/needed, my users (bless 'em!) want to go back to a free format text field for entering dates.

Whilst I'm reluctant to do this as it's caused data problems on the old application that I am currently upgrading (they have a tendency to enter all kinds of different formats), I may well have to bow to pressure and do this. Therefore I want to be able to validate the date string they enter and only allow them to enter an agreed format i.e. dd/mm/yyyy.

I looked at the isDate function but that just checks it's a date and doesn't seem to check if it is a specific format. If they entered "January 2005" instead of "20/01/2005" would this function return false?

Before saving the date to the database, I'm casting the date text to a datetime in the SQL statement, so do I need to enforce a certain format or will just enforcing a recognised date e.g. "20/01/2005" or "20 January 2005" be enough?

Thanks!
 
Regular expressions can be usfull here. This one will validate dates in the format you have described..

VB.NET:
(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d
 
Sorry I'm a bit lost with that! Do I need to compare the string that the user enters with the string you gave me? How would I do that?
 
Back
Top