我正在尝试创建密码检查脚本.我已经检查过电子邮件(对于不允许的字符),如下所示:
public function checkEmail($email)
{
if (filter_var($email, FILTER_VALIDATE_EMAIL))
return true;
else
return false;
}
所以我正在寻找一个密码验证功能,它检查密码至少有一个字母数字字符,一个数字字符,最少8个字符,并提供错误信息.
解决方法:
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);
}
编辑版本:http://www.cafewebmaster.com/check-password-strength-safety-php-and-regex