Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex - Date validation 1

Status
Not open for further replies.

Alphabin

Programmer
Dec 8, 2002
119
CA
I know it may have been already discussed in the past, but I need a simple regex to validate dates in the following format: yyyy-mm-dd


Thank you
 
a simple regex can't validate a date, merely the format of date

v.v. 2004-02-30

There are times when a regex shouldn't be used, and this looks like one of 'em, but I am prepared to eat humble pie

--Paul
 
What do you mean by validate? If you simply mean 'make sure it is 4 digits followed by a dash followed by 2 digits followed by a dash followed by 2 digits' then a simple regex would work fine. If you want to get into checking to make sure that the second two digits are between 1 and 12 or that if the second two digits are 2, then the last two digits must be between 1 and 28 unless the first 4 digits are divisible by 4 then between 1 and 29, then a regex will still work, but it would be easier IMO to split the string and validate each piece.

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
I'm simply looking basic regex to check if the date as the corrent format xxxx-xx-xx (x = digits) I don't wanna check if the numbers are valid or not...

Thank you guys
 
I think its as simple as
Code:
if ($date=m/dddd-dd-dd/) {
...
}

but i could be wrong

--Paul
 
I would have thought:
m/\d{4}-\d{1,2}-\d{1,2}/

But it will depend on if the day and month values will be padded or not.

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
Thank you guys for the help.

TomThumbKP: It's working perfectly.

Again, thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top