The Joel on Software Discussion Group (CLOSED)

A place to discuss Joel on Software. Now closed.

This community works best when people use their real names. Please register for a free account.

Other Groups:
Joel on Software
Business of Software
Design of Software (CLOSED)
.NET Questions (CLOSED)
TechInterview.org
CityDesk
FogBugz
Fog Creek Copilot


The Old Forum


Your hosts:
Albert D. Kallal
Li-Fan Chen
Stephen Jones

Ultimate regex date validation?

I've googled and googled, but still have not found the ultimate regex date validation.

I need something that will first try to correct the nonsense a user enters f.x. 1. May 2004 or 10/10/2005 or 1.2.2004 (attempt to handle US and EU formats), and then hand over the 'correct' string to the second phase, that does the actual validation.
Coward
Monday, October 17, 2005
 
 
Just parse the damn date - why "validate it" first?

If the parse works, you have the date.

If you have the date, you can assume that the input is correct.

Otherwise it is obviously invalid.
Arafangion Send private email
Monday, October 17, 2005
 
 
PHP has a strtotime() functions that does that. It's much more than a regex - see http://www.php.net/strtotime

I guess other platforms have something similar.
Sagi B Send private email
Monday, October 17, 2005
 
 
Yes, all that function does is return a date given an input string.

It doesn't first say "Hmm, does this look like it might be a date?"

No, it just gets a date.  It probably returns some null value if it can't, but at no point does it just do:

1) Get the input string
2) Check to see if it "looks" like a date.
3) Check to see if it "looks" like it can't possibly be a date.
4) Go and hand over the input string to the "real" date function, which actually gets the date from the string.
Arafangion Send private email
Monday, October 17, 2005
 
 
Recently I've been attempting to convert a particular database.

I would just like to mention that you should NOT allow both EU and US dates.

It's really awful when any style is allowed (For instance, Access 97 does this, afaik).

What date is the following: 99/10/10

9 oct 0010?
9 oct 1910?
9 oct 2010?
(And again for all the other permutations)

Typically what I find happens is that dates default to US, unless it can only possibly be EU, or something other, which is a royal pain in the **** for us EU users!

So, moral of the story is pick only one format, and use that.

For system tasks I like year-month-day, and for user tasks I like something localized and abstracted, yet configuable via system defaults.
Arafangion Send private email
Monday, October 17, 2005
 
 
Your database was storing dates as text fields, rather than using the native date type?

If you need to specify a date format, look at ISO 8601. In a nutshell it's YYYY-MM-DD, and if you add time it includes functionality for timezones.
Dennis Forbes Send private email
Monday, October 17, 2005
 
 
randall schwartz (of perl fame) says one cannot use a regex to validate/parse all date formats.
YaYaYa Send private email
Monday, October 17, 2005
 
 
If you have GNU date installed, just run in a subshell:

date -d '10:00 jan 1 2003' +%s

Or go read the source to GNU date.
Mike Schiraldi
Monday, October 17, 2005
 
 
>It doesn't first say "Hmm, does this look like it might be a date?"

And what's your problem with that? If it returns a date, then the thing obviously looked like a date. If it doesn't, then thing thing obviously didn't look like a date. Are you worried about the extra CPU cycles or something?

No regex can completely check a date for validity. Regexes can only do a certain limited form of pattern patching (read up on regular grammars), they aren't a magical cut-and-paste answer to all data validation problems
Matt Send private email
Monday, October 17, 2005
 
 
Matt, I was disputing the sense of first looking to see if the date was valid, then parsing it.

In other words, the useage of some regexp to check to see if it "looks" ok, then the useage of some real parser to get the real date.

email addresses are another common victim of regexp matches.
Arafangion Send private email
Monday, October 17, 2005
 
 
I found this one (date only, and only accurate for 1/1/1600 and later) after a short amount of Googling:

^(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$

http://www.regexlib.com/REDetails.aspx?regexp_id=113

If you need time as well, this one (by the same author, and valid for 01/01/0001 or later) will handle that also:

(?=\d)^(?:(?!(?:10\D(?:0?[5-9]|1[0-4])\D(?:1582))|(?:0?9\D(?:0?[3-9]|1[0-3])\D(?:1752)))((?:0?[13578]|1[02])|(?:0?[469]|11)(?!\/31)(?!-31)(?!\.31)|(?:0?2(?=.?(?:(?:29.(?!000[04]|(?:(?:1[^0-6]|[2468][^048]|[3579][^26])00))(?:(?:(?:\d\d)(?:[02468][048]|[13579][26])(?!\x20BC))|(?:00(?:42|3[0369]|2[147]|1[258]|09)\x20BC))))))|(?:0?2(?=.(?:(?:\d\D)|(?:[01]\d)|(?:2[0-8])))))([-.\/])(0?[1-9]|[12]\d|3[01])\2(?!0000)((?=(?:00(?:4[0-5]|[0-3]?\d)\x20BC)|(?:\d{4}(?!\x20BC)))\d{4}(?:\x20BC)?)(?:$|(?=\x20\d)\x20))?((?:(?:0?[1-9]|1[012])(?::[0-5]\d){0,2}(?:\x20[aApP][mM]))|(?:[01]\d|2[0-3])(?::[0-5]\d){1,2})?$

http://www.regexlib.com/REDetails.aspx?regexp_id=761
Neyah
Thursday, October 20, 2005
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz