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

查看:966
本文介绍了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])(?=.*[\d\x])(?=.*[!$#%]).*$

我已经在网站上进行了测试和验证.但是,以自己的方式表现自己并相应地进行调整.这只是正则表达式的一个示例,您可以按照自己的方式进行操作.

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])(?=.*[\d\X])(?=.*[!$#%]).*$/|
               confirmed',

更新 如评论中的@NikK所述,在Laravel 5.6中,密码值应封装在数组方括号中,例如

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

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

我还没有在Laravel 5.6上进行测试,所以我信任@NikK,因此这些天我已经开始使用c#/.net,并且没有太多时间来使用Laravel.

I have not testing it on Laravel 5.6 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个字符内包含至少3个a-z或A-Z以及数字和特殊字符.
  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中的regex规则的自定义验证消息,以下是一些链接供您查看:

  • Laravel Validation custom message
  • Custom validation message for regex rule in Laravel?
  • Laravel custom validation messages

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

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