【发布时间】:2018-02-09 17:33:55
【问题描述】:
我一直在为注册表单创建一些验证,但在验证用户名时遇到了错误。
我对名字和姓氏都使用了“测试”输入,但当它应该允许输入时我仍然得到一个错误,因为
!preg_match('/^[a-z]*$/i', $value) 应该允许大小写字符吗?
另外,我得到的错误似乎只触发了名字而不是姓氏。
<?php
if (isset($_POST['submit'])){
require_once 'config.php';
$errors = [];
foreach($_POST as $field => $value){
if($field === 'firstname' || $field === 'lastname' && !preg_match('/^[a-z]*$/i', $value)){
$errors[] = "{$field} has invalid characters please try again.";
echo $error;
}
}
}
?>
【问题讨论】:
-
请张贴您的html
-
是条件逻辑错误。将 () 放在 or 子句周围
标签: php regex validation