php Laravel 密码验证规则

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31539727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:29:47  来源:igfitidea点击:

Laravel password validation rule

phplaravelvalidationlaravel-5

提问by Bharanikumar

How to added password validation rule in the validator?

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

Validation rule:

验证规则:

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

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

  • English uppercase characters (A – Z)
  • English lowercase characters (a – z)
  • Base 10 digits (0 – 9)
  • Non-alphanumeric (For example: !, $, #, or %)
  • Unicode characters
  • 英文大写字符 (A – Z)
  • 英文小写字符 (a – z)
  • 基数 10 位 (0 – 9)
  • 非字母数字(例如:!、$、# 或 %)
  • Unicode 字符

How to add above rule in the validator rule?

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

My Code Here

我的代码在这里

// 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);

    }

回答by maytham-???????

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

我在 Laravel 中遇到过类似的情况,并通过以下方式解决了它。

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

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

  • English uppercase characters (A – Z)
  • English lowercase characters (a – z)
  • Base 10 digits (0 – 9)
  • Non-alphanumeric (For example: !, $, #, or %)
  • Unicode characters
  • 英文大写字符 (A – Z)
  • 英文小写字符 (a – z)
  • 基数 10 位 (0 – 9)
  • 非字母数字(例如:!、$、# 或 %)
  • Unicode 字符

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

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

Your regular expression would look like this:

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

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

I have tested and validated it on thissite. 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.

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

So your final Laravel code should be like this:

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

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

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

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

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

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.

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

Note:

笔记:

  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
  1. 我已经在正则表达式站点和 Laravel 5 测试环境中对其进行了测试和验证,并且可以正常工作。
  2. 我使用过 min:6,这是可选的,但拥有反映不同方面的安全策略始终是一个好习惯,其中之一是最小密码长度。
  3. 我建议您使用密码确认以确保用户输入正确的密码。
  4. 在 6 个字符中,我们的正则表达式应包含至少 3 个 az 或 AZ 以及数字和特殊字符。
  5. 在进入生产环境之前,始终在测试环境中测试您的代码。
  6. 更新:我在这个答案中所做的只是正则表达式密码的例子

Some online references

一些网上参考

Regarding your custom validation message for the regex rule in Laravel, here are a few links to look at:

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

回答by James

This doesn't quite match the OP requirements, though hopefully it helps. With Laravel you can define your rules in an easy-to-maintain format like so:

这并不完全符合 OP 要求,但希望它有所帮助。使用 Laravel,您可以以易于维护的格式定义规则,如下所示:

    $inputs = [
        'email'    => 'foo',
        'password' => 'bar',
    ];

    $rules = [
        'email'    => 'required|email',
        'password' => [
            'required',
            'string',
            'min:10',             // must be at least 10 characters in length
            'regex:/[a-z]/',      // must contain at least one lowercase letter
            'regex:/[A-Z]/',      // must contain at least one uppercase letter
            'regex:/[0-9]/',      // must contain at least one digit
            'regex:/[@$!%*#?&]/', // must contain a special character
        ],
    ];

    $validation = \Validator::make( $inputs, $rules );

    if ( $validation->fails() ) {
        print_r( $validation->errors()->all() );
    }

Would output:

会输出:

    [
        'The email must be a valid email address.',
        'The password must be at least 10 characters.',
        'The password format is invalid.',
    ]

(The regex rules share an error message by default—i.e. four failing regex rules result in one error message)

(默认情况下,正则表达式规则共享一条错误消息——即四个失败的正则表达式规则会导致一条错误消息)

回答by Matthew Way

Sounds like a good job for regular expressions.

听起来像正则表达式的好工作。

Laravel validation rules support regular expressions. Both 4.X and 5.X versions are supporting it :

Laravel 验证规则支持正则表达式。4.X 和 5.X 版本都支持它:

This might help too:

这也可能有帮助:

http://www.regular-expressions.info/unicode.html

http://www.regular-expressions.info/unicode.html