Click to See Complete Forum and Search --> : Preg_match Pattern


Sheldon
05-12-2008, 07:17 AM
Hi,

I am wondering if some one can help me match some text.

I am trying to make sure that a user is submitting a valid date format.

I want them to submit the format mm/dd/yyy.

Can any one help me with the REG_EX or what ever they think would work best to match this ?


Thanks

sstalder
05-12-2008, 08:51 AM
This should work.

function isValidDate($dte)
{
if (preg_match("/^(\d{2})\/(\d{2})\/(\d{2})$/", $dte, $matches)) {
if (checkdate($matches[2], $matches[3], $matches[1])) {
return true;
}
}
return false;
}

Sheldon
05-12-2008, 05:47 PM
Thanks.

It seams to return false each time ?

andre4s_y
05-12-2008, 08:24 PM
$pattern = "/^(?:[0][1-9]|[1][0|1|2])\/(?:[0][1-9]|[1|2][\d]|[3][0|1])\/\d{3}$/";
or replace the last \d{2} with \d{3} in sstalder's code above.

Why 3 digits of year??

Sheldon
05-12-2008, 08:57 PM
the 3 yyy's was a type, it was meant to be 4.

Thanks, I will try that now.

skywalker2208
05-12-2008, 09:40 PM
Instead of using a regular expression try this.
checkdate function (http://www.php.net/checkdate)