Regex Validation in Laravel 5.2

Deadly 提交于 2019-12-03 10:19:35

Use:

return [
    'password' => [
        'required',
        'confirmed',
        'min:8',
        'max:50',
        'regex:/^(?=.*[a-z|A-Z])(?=.*[A-Z])(?=.*\d)(?=.*(_|[^\w])).+$/',
    ]
];

Firstly, you do not need to check the confirmation separately. Just use the confirmed rule.

The expression you were using was invalid, and had nothing to do with what you wanted. I do suggest you do some research on regular expressions.

Due to the fact that the expression shown above uses pipes (|), you can specify the rules using an array.

Edit: You could also use this expression, which appears to have been tested a little more thoroughly.

/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\d\X])(?=.*[!$#%]).*$/

You might want to check the PasswordStrengthPackage. It registers new validation rules that do what you need and are much more readable than a regular expression. So in your case you can have this:

return [
    'Password' => 'required|min:8|max:100|case_diff|numbers|letters|symbols|confirmed'
];

The Password_confirmation rule is not needed as long as the confirmation value is present and you add the confirmed rule for the Password field.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!