18

我正在尝试创建密码检查脚本。我已经检查了电子邮件(对于不允许的字符),如下所示:

  public function checkEmail($email)
  {
    if (filter_var($email, FILTER_VALIDATE_EMAIL))
      return true;
    else
      return false;   
  }

所以我正在寻找一种密码验证功能,它可以检查密码至少有一个字母数字字符、一个数字字符和至少 8 个字符,并且还提供错误消息。

4

1 回答 1

81
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

于 2012-05-25T10:47:11.647 回答