我正在进行日期验证,但它一直给我一个错误消息
注意:未定义的变量:第15行的C:\ xampp \ htdocs \ signup.php中的部分
if (empty($_POST['birthday']))
// the user's date of birth cannot be a null string
$errors[] = "You must supply a date of birth.";
else if (!strpos("^([0-9]{2})/([0-9]{2})/([0-9]{4})$", $_POST['birthday'], $parts))
// Check the format
$errors[] = "The date of birth is not a valid date in the " .
"format DD/MM/YYYY";
elseif (!checkdate($parts[2],$parts[1],$parts[3]))
$errors[] = "The date of birth is invalid. " .
"Please check that the month is between 1 and 12, " .
"and the day is valid for that month.";
elseif (intval($parts[3]) < 1890)
// Make sure that the user has a reasonable birth year
$errors[] = "You must be alive to use this service.";
答案 0 :(得分:1)
我昨晚刚回答了一些问题。你的工作太辛苦了!
PHP已经有许多不同的验证器。使用checkdate进行验证而不是REGEX
答案 1 :(得分:0)
问题是您使用了错误的函数进行正则表达式匹配。
使用preg_match("/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/", $_POST['birthday'], $parts)
答案 2 :(得分:0)
您使用strpos
是错误的。查看strpos manual。
第三个参数可用于指定从哪里开始,而不是为正则表达式模式匹配指定目标数组,你在哪里得到了strpos可以做到的想法? strpos根本不支持正则表达式。
您应该使用preg_match_all代替,例如:
preg_match_all("{^([0-9]{2})/([0-9]{2})/([0-9]{4})$}", $_POST['birthday'], $parts);
答案 3 :(得分:0)
你有一个致命的错误,即变量$ part仅在第一个if语句中起作用。 尝试在第一个if语句后添加:
$part = strpos("^([0-9]{2})/([0-9]{2})/([0-9]{4})$", $_POST['birthday'])
您还需要尝试:LiveValidation以获得更好的验证
或使用此功能:checkdate