【问题标题】:PHP Validation regex throws exception with what should be valid dataPHP验证正则表达式抛出异常,应该是有效数据
【发布时间】:2018-03-22 14:14:48
【问题描述】:

我遇到了 php 服务器端验证的问题。我正在尝试验证应遵循以下规则的密码字段:8 到 15 个符号,至少 1 个大写字母,至少 1 个特殊符号,至少 3 个字母和至少 2 个数字。除了密码之外,每个验证都通过了,我不知道为什么。我的php代码是:

if (isset($_POST['register'])) {

        $form = $_POST;
        $username = $form['username'];
        $password = $form['password'];
        $confirmPass = $form['confirmPass'];
        $firstName = $form['firstName'];
        $lastName = $form['lastName'];
        $address = $form['address'];
        $email = $form['email'];
        $age = $form['age'];
        $phone = $form['phone'];
//Retrieve the field values from our registration form.
        $username = !empty($_POST['username']) ? trim($_POST['username']) : null;
        $password = !empty($_POST['password']) ? trim($_POST['password']) : null;

//TO ADD: Error checking (username characters, password length, etc).
//Basically, you will need to add your own error checking BEFORE
//the prepared statement is built and executed.

        //Validations username
        if (strlen($username) < 4 || strlen($username) > 8 || empty($username)) {
            throw new Exception("User name must be between 4 an 8 symbols.");
        }
        $patern = '#^[A-Za-z0-9]+$#';
        if (!preg_match($patern, $username)) {
            throw new Exception("User name must not contains Special characters.");
        }
        //Validation password
        if (strlen($password) < 8 || strlen($password) > 15 || empty($password)) {
            throw new Exception("Password must be between 8 an 15 symbols.");
        }
        $patern = '#^(?=(.*\d){2,})(?=.*[A-Z]{1,})(?=.*[a-zA-Z]{2,})(?=.*[!@~#$%^&?]{1,})[0-9a-zA-Z!@~#?$^%&`]+$#';
        if (!preg_match($patern, $password)) {
            throw new Exception("Password must contains at least 1 special symbol at least 1 uppercase letter at least 2 numbers at least 3 letters.");
        }
        if ($password != $confirmPass) {
            throw new Exception("Password do not match.");
        }
        //Validation email
        $patern = '#^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$#';
        if (!preg_match($patern, $email)) {
            throw new Exception("Please fill valid email.");
        }
        //Validation phone
        if (strlen($phone) != 10) {
            throw new Exception("Phone must be 10 numbers.");
        }
        //Validation age
        if (intval($age) < 18) {
            throw new Exception("You must be at least 18 years old");
        }
        //Validation check
        if (!isset($_POST['gdpr'])) {
            throw new Exception("You must agree with GDPR.");
        }

        if (!isset($_POST['agreement'])) {
            throw new Exception("You must agree with the terms and conditions.");
        }
    }

【问题讨论】:

  • 您可能会受益于Reference - Password Validation。不要将密码限制为特定的特殊字符,实际上没有必要这样做,而且您会降低它的安全性。此外,您应该允许使用 Unicode 字符,以便用户在选择这样做时可以使他们的密码更加安全,例如使用 ô 或任何其他非 ASCII 符号。此外,{1,}+ 量词相同。您还需要将其更改为(?:.*[a-zA-Z]){2,}
  • 你的正则表达式有问题吗?如果是这样,请在该网站上查看regex101.com
  • 在网站 regex101 上没有,正则表达式工作得很好并且匹配正确,这似乎是其他地方的问题。
  • 为什么将密码限制为 15 个字符?耶稣,不,不要那样做。
  • 这是一个内部培训项目,产品负责人定义了规则,反正密码验证很奇怪。

标签: php regex validation


【解决方案1】:

# 作为分隔符和 # 在正则表达式的内容中导致过早结束分隔符,因此修饰符错误。

而不是 # 使用 / 作为分隔符来成功 preg_match() 而不修改正则表达式的内容。

喜欢来自:

$patern = '#^(?=(.*\d){2,})(?=.*[A-Z]{1,})(?=.*[a-zA-Z]{2,})(?=.*[!@~#$%^&?]{1,})[0-9a-zA-Z!@~#?$^%&`]+$#';

收件人:

$patern = '/^(?=(.*\d){2,})(?=.*[A-Z]{1,})(?=.*[a-zA-Z]{2,})(?=.*[!@~#$%^&?]{1,})[0-9a-zA-Z!@~#?$^%&`]+$/';

【讨论】:

  • 虽然这确实正确回答了问题,但添加 why 以使其更完整。
  • 如果您更喜欢旧的 '#',那么只需在正则表达式中转义 #,就像 [!@~\#$%^&?] 中的 \# 一样
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-13
  • 1970-01-01
  • 1970-01-01
  • 2017-02-07
  • 1970-01-01
  • 2010-10-27
  • 1970-01-01
相关资源
最近更新 更多