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.

Chase
  • 9,289
  • 5
  • 51
  • 77
  • Is this a list of requirements you've come up with independently, or is this being imposed on you? Most of the time these practices degrade security as people have a hard time remembering passwords that differ from their usual ones, so it goes on paper notes and such. – tadman Oct 08 '15 at 20:00
  • [another solution](http://stackoverflow.com/questions/32502446/codeigniter-form-validation-setting-strong-password-validation-rule-in-an-array/32504388#32504388) create a callback function. and add authentication what ever you want. in function when password validated just return `TRUE` or `FALSE` then check the response in you're password validation. – Kaleem Ullah Oct 09 '15 at 06:51

2 Answers2

2

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!

  • This doesnt account for numbers, nor does it allow individual messages for why a password didnt pass validation. But it does offer options, so if someone else isnt able to come up with how to do it allowing for customized messages per rule, ill accept your answer. – Chase Oct 09 '15 at 20:31
0

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);
    }
AkshayBandivadekar
  • 839
  • 10
  • 18
  • As i said to jorge, i would like to be able to separate out the requirements so i can give back specialized messages – Chase Oct 11 '15 at 02:07