How to added password validation rule in the validator?
Validation rule:
The password contains characters from at least three of the followi
Sounds like a good job for regular expressions.
Laravel validation rules support regular expressions. Both 4.X and 5.X versions are supporting it :
This might help too:
http://www.regular-expressions.info/unicode.html
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:
$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)
A Custom Laravel Validation Rule will allow developers to provide a custom message with each use case for a better UX experience.
php artisan make:rule IsValidPassword
namespace App\Rules;
use Illuminate\Support\Str;
use Illuminate\Contracts\Validation\Rule;
class isValidPassword implements Rule
{
/**
* Determine if the Length Validation Rule passes.
*
* @var boolean
*/
public $lengthPasses = true;
/**
* Determine if the Uppercase Validation Rule passes.
*
* @var boolean
*/
public $uppercasePasses = true;
/**
* Determine if the Numeric Validation Rule passes.
*
* @var boolean
*/
public $numericPasses = true;
/**
* Determine if the Special Character Validation Rule passes.
*
* @var boolean
*/
public $specialCharacterPasses = true;
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$this->lengthPasses = (Str::length($value) >= 10);
$this->uppercasePasses = (Str::lower($value) !== $value);
$this->numericPasses = ((bool) preg_match('/[0-9]/', $value));
$this->specialCharacterPasses = ((bool) preg_match('/[^A-Za-z0-9]/', $value));
return ($this->lengthPasses && $this->uppercasePasses && $this->numericPasses && $this->specialCharacterPasses);
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
switch (true) {
case ! $this->uppercasePasses
&& $this->numericPasses
&& $this->specialCharacterPasses:
return 'The :attribute must be at least 10 characters and contain at least one uppercase character.';
case ! $this->numericPasses
&& $this->uppercasePasses
&& $this->specialCharacterPasses:
return 'The :attribute must be at least 10 characters and contain at least one number.';
case ! $this->specialCharacterPasses
&& $this->uppercasePasses
&& $this->numericPasses:
return 'The :attribute must be at least 10 characters and contain at least one special character.';
case ! $this->uppercasePasses
&& ! $this->numericPasses
&& $this->specialCharacterPasses:
return 'The :attribute must be at least 10 characters and contain at least one uppercase character and one number.';
case ! $this->uppercasePasses
&& ! $this->specialCharacterPasses
&& $this->numericPasses:
return 'The :attribute must be at least 10 characters and contain at least one uppercase character and one special character.';
case ! $this->uppercasePasses
&& ! $this->numericPasses
&& ! $this->specialCharacterPasses:
return 'The :attribute must be at least 10 characters and contain at least one uppercase character, one number, and one special character.';
default:
return 'The :attribute must be at least 10 characters.';
}
}
}
Then on your request validation:
$request->validate([
'email' => 'required|string|email:filter',
'password' => [
'required',
'confirmed',
'string',
new isValidPassword(),
],
]);
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:
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 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.
So your final Laravel code should be like this:
'password' => 'required|
min:6|
regex:/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\d\x])(?=.*[!$#%]).*$/|
confirmed',
Update As @NikK in the comment mentions, in Laravel 5.5 and newer 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']
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.
Note:
Some online references
Regarding your custom validation message for the regex rule in Laravel, here are a few links to look at: