I'm using the following regex to parse a date in dd/mm/yyyy
format:
^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$
I've checked it on strfriend and it all looks ok. However, when testing for it in PHP with preg_match
it doesn't recognize it:
if(!preg_match("/^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$/",trim($_POST['dob']))){
$error .= "You must enter a valid date of birth.";
}
This happens with input such as 29/10/1987
and 01-01-2001
, and I'm not sure why it doesn't work!
I also get the following warning:
Warning: preg_match() [function.preg-match]: Unknown modifier '.' in /home/queensba/public_html/workers/apply.php on line 18
which I'm not sure how to interpret.
If you use '/' in within your regex, you may not start/end the regular expression with it. Just replace it with '#' for example.
if(!preg_match("#^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$#",trim($_POST['dob']))){
$error .= "You must enter a valid date of birth.";
}
(BTW, Modifiers would come after the final delimiter '#'. So the warning appeared because PHP thought the regex would end after the second '/'.)