Laravel 5.2中的正则表达式验证 [英] Regex Validation in Laravel 5.2

查看:124
本文介绍了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

  1. 至少一个小字符
  2. 至少一个大字符
  3. 至少一个数字
  4. 至少一个特殊字符
  5. 最少8个字符
  1. atleast one small char
  2. atleast one big char
  3. atleast one number
  4. atleast one special char
  5. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆