Its is very important to force a user to create very strong password in your every project. Laravel does not provide out of box thats why i am here to provide you the way to create a strong password.

Validation Rule:
  • At least 8 characters
  • At least 1 lowercase letter
  • At least 1 uppercase letter
  • At least 1 numerical number
  • At least 1 special character
'password' => [
   'required',
   'min:8',
   'regex:/^(?=.?[A-Z])(?=.?[a-z])(?=.?[0-9])(?=.?[#?!@$%^&*-]).{8,}$/'
   'confirmed'
 ]

If you want to show a proper validation error message. you can below code.

You need to pass a second parameter in your laravel validation rule.

The password must be alteast 8 characters and must be combination of uppercase, lowercase, special character and a digit.
[
 "password.regex": "The password must be alteast 8 characters and must be combination   of uppercase, lowercase, special character and a digit."
]

Here is the full code using laravel validator.

$validator =  Validator::make($request->all(), [
   'password' => [
     'required',
     'min:8',
     'regex:/^(?=.?[A-Z])(?=.?[a-z])(?=.?[0-9])(?=.?[#?!@$%^&*-]).{8,}$/'
     'confirmed'
   ]
  ],
  [
  "password.regex": "The password must be alteast 8 characters and must be combination of uppercase, lowercase, special character and a digit."
  ]);

Another solution

If you want to handle the cases of password validation seperatly then you can use below code.

'password' => [
   'required',
   'min:8',             // must be at least 8 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
  ]

Enjoy ☺️

Author

I am a Laravel developer with some other skills.

Write A Comment