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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

REGEX Pattern help 1

Status
Not open for further replies.

MattJenneson

Programmer
Sep 12, 2002
8
GB
Hey Guys, not sure if this is the right forum, but here goes...


I'm new to regex patterns, I'm using a simple one to validate a date. dd/mm/yyyy.

/^[\d]{2}\/[\d]{2}\/[\d]{4}$/

Can someone help me figure out how to limit the day portion to 01 - 31 and the month portion to 01 - 12. I have also provided a date picker, but would be nice if i could stop people keying 33/13/2008.

Any help gratefully appreciated, or links to online resources.

Thanks

Matt
 
have a look at checkdate().

Code:
function validateDate($date){
 list($day, $month, $year) = explode ('/'. $date);
 return checkdate($month, $day, $year);
}

 
The pattern for dd ranging (01-31), for mm ranging (01-12), however insufficient be it in the application, is this.
[tt]/^([0][1-9]|[12][0-9]|[3][01])\/([0][1-9]|[1][0-2])\/[0-9]{4}$/[/tt]
With a bit of restriction on the yyyy using the construction similar to dd and mm, you may get a closer and useful pattern for the application for a coarse screening.
 
Matt

a regex is not the way to validate a date. despite the fact that loading the PCRE engine is inefficient from a processor perspective, it's also much slower and, frankly, does not validate a date. for example if the month is February, the day will still validate unless you put loads of procedural code in to test for the various possibilities.

php has a compiled, built in method for checking dates. this is what i advise you to use. if you need client side validation, consider either writing some procedural javascript code to perform the validation (again, regex is NOT the right answer here) or use some ajax to call a client side script.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top