Laravel 5.2中的正则表达式验证 [英] Regex Validation in Laravel 5.2
问题描述
以下是我的密码规则:
return [
'Password' => 'required|min:8|max:100|regex:[a-z{1}[A-Z]{1}[0-9]{1}]',
'Password_confirmation' => 'required|min:8|max:100|regex:[a-z{1}[A-Z]{1}[0-9]{1}]',
];
我正在尝试添加必须具有的规则
I am trying to add the rule such that it must have
- 至少一个小字符
- 至少一个大字符
- 至少一个数字
- 至少一个特殊字符
- 最少8个字符
- atleast one small char
- atleast one big char
- atleast one number
- atleast one special char
- min 8 chars
我尝试过此操作,并且在 regex测试仪软件上可以运行required|confirmed|min:8|max:100|regex:/^[\w]{1,}[\W]{1,}$/
.但不确定为什么它在 Laravel
I tried this and it works required|confirmed|min:8|max:100|regex:/^[\w]{1,}[\W]{1,}$/
, on a regex tester software . but not sure why it does not work in Laravel
我想念什么吗?
推荐答案
使用:
return [
'password' => [
'required',
'confirmed',
'min:8',
'max:50',
'regex:/^(?=.*[a-z|A-Z])(?=.*[A-Z])(?=.*\d)(?=.*(_|[^\w])).+$/',
]
];
首先,您无需单独检查确认.只需使用confirmed
规则.
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.
编辑:您还可以使用此表达式,该表达式似乎已经过更彻底的测试.
You could also use this expression, which appears to have been tested a little more thoroughly.
/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\d\X])(?=.*[!$#%]).*$/
这篇关于Laravel 5.2中的正则表达式验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!