【问题标题】:Laravel password validation rule with errorLaravel 密码验证规则有错误
【发布时间】:2020-07-17 19:02:49
【问题描述】:

验证规则:

密码包含来自以下五类中的至少三类的字符:

英文大写字符 (A – Z) 英文小写字符 (a – z) 以 10 位为基数 (0 – 9) 非字母数字(例如:!、$、# 或 %) Unicode 字符

我想使用上面的密码验证规则,我的代码是这样的。但我仍然只能通过数字注册到我的系统。

 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'min:8', 'regex:/[a-z]/', 'regex:/[A-Z]/', 'regex:/[0-9]/', 'regex:/[@$!%*#?&]/','confirmed'],

【问题讨论】:

    标签: laravel


    【解决方案1】:

    你有多个正则表达式规则的问题,一个正则表达式可以概括你所有的。

    ^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$
    

    这意味着匹配:

    • {8,} 最少八个字符(因此不需要min:8
    • A-Za-z上下任意字母至少一个
    • @$!%*#?&] 至少一个特殊字符。
    • \d number/s

    所以你的规则现在应该是这样的:

    'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'regex:^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$','confirmed'],
    

    【讨论】:

    • 不匹配 uni-codes 并且不尊重 3 out of 5 条件
    猜你喜欢
    • 2015-10-10
    • 2016-05-13
    • 2021-10-09
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    • 2016-12-17
    相关资源
    最近更新 更多