Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Regex According to Jeff

The right tool for the right job

Date Validation

Lets assume you want to store a valid date.  I'll further assume that you'll want to do Date things on that date (either in SQL or in code or somewhere, but you probably want to use it).

Religious tool fanatics will say “regex is the only way to go”.  It might be, but I think there is a much nicer way to do this.  Use the language's (of your choice) built in tools, or write a custom method to do this for you.

I'd do something like this:

public bool IsDate(string date)
{
 DateTime dt;
 bool isDate = true; //assume tbDate.Text is a date
 try
 {
  dt = DateTime.Parse(date);
 }
 catch (FormatException efx)
 {
  //failed, so not a date.
  isDate = false;
 }
 catch (ArgumentOutOfRangeException aoex)
 {
  // failed, so not a date.
  isDate = false;
 }
 return isDate;
}

Use that function in a custom validator, or put it in a utility class and use it all the time.  It works for a wide variety of formats, as shown by this impromptu test:

string[] dates = new string [] { 
  "4/4/2003","4/4/03","May 4, 2003", "4 May 2003", "2003-04-04", 
  "4/41/2003","14/4/03","Masy 4, 2003", "4 Maya 2003", "20023-04-04" 
  };
 foreach (string date in dates)
 {
  bool isDate = IsDate(date);
  litResults.Text += date + " => ";
  if (isDate)
   litResults.Text += " Valid Date
\n"
; else litResults.Text += " Invalid Date
\n"
; }

Generating the following output:

4/4/2003 => Valid Date
4/4/03 => Valid Date
May 4, 2003 => Valid Date
4 May 2003 => Valid Date
2003-04-04 => Valid Date
4/41/2003 => Invalid Date
14/4/03 => Invalid Date
Masy 4, 2003 => Invalid Date
4 Maya 2003 => Invalid Date
20023-04-04 => Invalid Date

Just trying to check within a single format is difficult enough with regex, trying to accept all those formats would give anyone a headache.  You also win by doing this in code if your language of choice supports localization.

No, every post won't be about avoiding regex, I just have to get a few things off my chest.  Remember my grain of salt.  It's all about the right tool for the right job.

Sponsor
Published Tuesday, November 11, 2003 1:39 PM by jeffrey
Filed under:

Comments

 

jeffrey said:


Thanks for that Jeff. Keep ranting, you'll fill my toolbox in no time ;-)
November 11, 2003 2:04 PM
 

jeffrey said:

Hi Jeff !

Why would "14/4/03" be an incorrect date ? Here in France, our date format is "d/m/y" and "14/4/03" means 14th of April 2003. Do you have any advice on globalization and internationalization ?
November 11, 2003 4:18 PM
 

jeffrey said:

with internationalisation, there are a couple of different possibility's.

One thing to be aware of is the culture type of the website and the culture type of the database and if they match.

If they match then your all clear, you can always extract the day/ month through functions.

In .Net this made fairly easy because you can set the culture and convert things that way.

Another way is to use a numeric format.

like in:

20031010,195710
yyyymmdd,hhmmss

November 12, 2003 3:28 AM
 

jeffrey said:

Julien --

Unfortunately I have no experience in globalization or i18n.

I contract almost exclusively for the US Govt, so my needs are--or at least have been--much more specific.

I'll throw this onto my queue though, and find out how to deal with i18n and my little IsDate method.
November 12, 2003 8:03 AM
 

jeffrey said:

Try Catch blocks are expensive. Why not consolidate it into a single generic Exception object?

My 2 cents - to me, its either a date, or it isnt. The reasons why it isnt a date are really limited.
March 15, 2004 10:01 AM
 

jeffrey said:

From the above example I could move to catch a single Exception and determine it's not a date. However, not coded, but certainly open for expansion I could use each specific exception to give better feedback to the user, mostly with the ArgumentOutOfRangeException but either way really.
March 15, 2004 1:12 PM
 

jeffrey said:

Julien, my guess is that 14/4/03 being marked as an error is actually the .NET Globalization classes WORKING. 14/4/03 would probably work if Thread.CurrentCulture was first set to FR-fr, or another European culture. It probably fails because in EN-us (Jeff's default Culture), that is an invalid date format, because there is no 14th month...
April 1, 2004 11:26 PM
 

TrackBack said:

January 12, 2004 11:33 AM
 

TrackBack said:

January 7, 2005 9:16 AM
 

TrackBack said:

January 10, 2005 8:47 AM
 

Date Validation said:

December 5, 2007 5:27 PM
 

http://regexadvice.com/blogs/jschoolcraft/archive/2003/11/11/262.aspx said:

March 23, 2008 3:25 AM
Anonymous comments are disabled