I'm using this string to validate a date field in dd/mm/yyyy' and 'dd-mm-yyyy format:
'/^(0?[1-9]|[12][0-9]|3[01])[\/\.- ](0?[1-9]|1[0-2])[\/\.- ](19|20)\d{2}$/'
but I get this error
Warning: preg_match() [function.preg-match]: Unknown modifier '\' i开发者_开发百科n /var/www/...fields_lib.php on line 102
Keep in mind the the above string is entered on a web application optional validation form field without any delimeter, because I think the form embed the delimeters itself. For other validation types like, integer and decimal numbers, I had to remove the delimeters for the validation to work on this specific form.
Any ideas ?
Thank You
You don't need the backslashes in character classes, so your regex should read (in part) [/. -]
. Also note that the space and the dash have switched spaces, because [.- ]
would be interpreted as "any character between .
and .
I don't get that error; instead I get a "range out of order" error, for the reason @CanSpice gave. To get the other error, I have to remove the first backslash in the character class ([/\.- ]
instead of [\/\.- ]
). Then it interprets the /
as a regex delimiter, and it expects the next character to be a modifier (like i
for case-insensitive, or m
for multiline).
So you've got two problems: the -
being treated as a range operator, and the /
being treated as a regex delimiter. You can deal with both problems by escaping the offending characters with backslashes (i.e., [\/.\- ]
), but each problem has a more elegant solution. If you move the -
to the first or last position where it couldn't form a range, it gets treated as a literal -
. As for the /
, you can use something else for the regex delimiter. For example:
'~^(0?[1-9]|[12][0-9]|3[01])[/. -](0?[1-9]|1[0-2])[/. -](19|20)\d{2}$~'
FYI, the .
never needed to be escaped at all. In character classes, most regex metacharacters lose their special meanings. You just happened to run afoul of two special cases. :-/
精彩评论