Laravel 密码验证规则 [英] Laravel password validation rule

查看:55
本文介绍了Laravel 密码验证规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在验证器中添加密码验证规则?

How to added password validation rule in the validator?

验证规则:

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

The password contains characters from at least three of the following five categories:

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

如何在验证器规则中添加上述规则?

How to add above rule in the validator rule?

我的代码在这里

// create the validation rules ------------------------
    $rules = array(
        'name'             => 'required',                        // just a normal required validation
        'email'            => 'required|email|unique:ducks',     // required and must be unique in the ducks table
        'password'         => 'required',
        'password_confirm' => 'required|same:password'           // required and has to match the password field
    );

    // do the validation ----------------------------------
    // validate against the inputs from our form
    $validator = Validator::make(Input::all(), $rules);

    // check if the validator failed -----------------------
    if ($validator->fails()) {

        // get the error messages from the validator
        $messages = $validator->messages();

        // redirect our user back to the form with the errors from the validator
        return Redirect::to('home')
            ->withErrors($validator);

    }

推荐答案

我在 Laravel 中也遇到过类似的场景,通过以下方式解决.

I have had a similar scenario in Laravel and solved it in the following way.

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

The password contains characters from at least three of the following five categories:

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

首先,我们需要创建一个正则表达式并验证它.

First, we need to create a regular expression and validate it.

您的正则表达式如下所示:

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

我已经在这个站点上对其进行了测试和验证.然而,以你自己的方式表现你自己并相应地调整.这只是正则表达式的一个例子,你可以按照你想要的方式进行操作.

I have tested and validated it on this site. Yet, perform your own in your own manner and adjust accordingly. This is only an example of regex, you can manipluated the way you want.

所以你最终的 Laravel 代码应该是这样的:

'password' => 'required|
               min:6|
               regex:/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[dx])(?=.*[!$#%]).*$/|
               confirmed',

更新正如评论中的@NikK 所提到的,在 Laravel 5.5 及更高版本中,密码值应封装在数组方括号中,如

Update As @NikK in the comment mentions, in Laravel 5.5 and newer the the password value should encapsulated in array Square brackets like

'password' => ['required', 
               'min:6', 
               'regex:/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[dx])(?=.*[!$#%]).*$/', 
               'confirmed']

我没有在 Laravel 5.5 上测试过,所以我相信 @NikK,因此我这些天已经转向使用 c#/.net 并且没有太多时间在 Laravel 上.

I have not testing it on Laravel 5.5 so I am trusting @NikK hence I have moved to working with c#/.net these days and have no much time for Laravel.

注意:

  1. 我已经在正则表达式站点和 Laravel 5 测试环境中对其进行了测试和验证,并且可以正常工作.
  2. 我使用过 min:6,这是可选的,但拥有反映不同方面的安全策略始终是一个好习惯,其中之一是最小密码长度.
  3. 我建议您使用确认密码以确保用户输入正确的密码.
  4. 在 6 个字符内,我们的正则表达式应至少包含 a-z 或 A-Z 以及数字和特殊字符中的 3 个.
  5. 在进入生产环境之前,始终在测试环境中测试您的代码.
  6. 更新:我在这个答案中所做的只是正则表达式密码的示例
  1. I have tested and validated it on both the regular expression site and a Laravel 5 test environment and it works.
  2. I have used min:6, this is optional but it is always a good practice to have a security policy that reflects different aspects, one of which is minimum password length.
  3. I suggest you to use password confirmed to ensure user typing correct password.
  4. Within the 6 characters our regex should contain at least 3 of a-z or A-Z and number and special character.
  5. Always test your code in a test environment before moving to production.
  6. Update: What I have done in this answer is just example of regex password

一些在线参考资料

  • http://regex101.com
  • http://regexr.com (another regex site taste)
  • https://jex.im/regulex (visualized regex)
  • http://www.pcre.org/pcre.txt (regex documentation)
  • http://www.regular-expressions.info/refquick.html
  • https://msdn.microsoft.com/en-us/library/az24scfc%28v=vs.110%29.aspx
  • http://php.net/manual/en/function.preg-match.php
  • http://laravel.com/docs/5.1/validation#rule-regex
  • https://laravel.com/docs/5.6/validation#rule-regex

关于 Laravel 中正则表达式规则的自定义验证消息,这里有几个链接供您查看:

这篇关于Laravel 密码验证规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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