更新时间:2022-01-10 22:51:53
你的正则表达式没有工作,因为你有>未转义 /
分隔符。
Your regex didn't work because you had unescaped /
delimiter.
正则表达式将以 YYYY-MM-DD
如下:
^(19|20)\d\d[\-\/.](0[1-9]|1[012])[\-\/.](0[1-9]|[12][0-9]|3[01])$
它将验证年份以 19开始
或 20
,该月份不超过 12
而不等于 0
而且该日期不大于 31
并且不等于 0
。
It will validate that the year starts with 19
or 20
, that the month is not greater than 12
and doesn't equal 0
and that the day is not greater than 31
and doesn't equal 0
.
使用您的初始示例,您可以这样测试:
Using your initial example, you could test it like this:
$date_regex = '/^(19|20)\d\d[\-\/.](0[1-9]|1[012])[\-\/.](0[1-9]|[12][0-9]|3[01])$/';
$hiredate = '2013-14-04';
if (!preg_match($date_regex, $hiredate)) {
echo '<br>Your hire date entry does not match the YYYY-MM-DD required format.<br>';
} else {
echo '<br>Your date is set correctly<br>';
}