Bya*_*gan 16 php passwords validation
我正在尝试创建密码检查脚本.我已经检查过电子邮件(对于不允许的字符),如下所示:
public function checkEmail($email)
{
if (filter_var($email, FILTER_VALIDATE_EMAIL))
return true;
else
return false;
}
Run Code Online (Sandbox Code Playgroud)
所以我正在寻找一种密码验证功能,它可以检查密码是否至少包含一个字母数字字符和一个数字字符,并且至少包含8个字符,并且还提供错误消息.
Jer*_*oen 66
public function checkPassword($pwd, &$errors) {
$errors_init = $errors;
if (strlen($pwd) < 8) {
$errors[] = "Password too short!";
}
if (!preg_match("#[0-9]+#", $pwd)) {
$errors[] = "Password must include at least one number!";
}
if (!preg_match("#[a-zA-Z]+#", $pwd)) {
$errors[] = "Password must include at least one letter!";
}
return ($errors == $errors_init);
}
Run Code Online (Sandbox Code Playgroud)
编辑版本:http://www.cafewebmaster.com/check-password-strength-safety-php-and-regex