简体   繁体   中英

How to validate passwords with Laravel 5.1

I would like to validate a password so that it meets the following:

  • is at least 8 characters long
  • contains at least 1 uppercase letter
  • contains at least 1 lowercase letter
  • contains at least 1 special character
  • matches a confirmation field

Currently i have:

$validator = \Validator::make($request, [
    'password' => 'min:8|confirmed'
],[
    'password.confirmed' => 'Both `password` and `password_confirmation` are required',
    'password.min' => 'The password must contain at least 8 characters'
]);

This will take care of the min length and the confirmation but i can find a good way to do the other check. Any help will be appreciated.

I think you can use a regex to do the trick: http://laravel.com/docs/5.1/validation#rule-regex

You can use like this:

['password' => ['regex:^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=!?]).*$']]

Hope it helps in someway!

Try this it works :)

   $rules = array(
        'username' => 'required',
        'email' => 'required | between:5,100 | email ',
        'password' => 'required | confirmed |regex:/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()+]+.*)[0-9a-zA-Z\w!@#$%^&*()+]{8,}$/'
    );

    $input = Input::all();

    $validation = Validator::make($input, $rules);

    if ($validation->fails()){
        //if validation fails redirect with inputs
        return redirect()->route('/')->withInput()
            ->withErrors($validation);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM