Skip to content

Instantly share code, notes, and snippets.

@dwaard
Created June 18, 2020 07:52
Show Gist options
  • Save dwaard/a76164a6b2b389f7586cac43a8fa6e86 to your computer and use it in GitHub Desktop.
Save dwaard/a76164a6b2b389f7586cac43a8fa6e86 to your computer and use it in GitHub Desktop.
A strong password custom validation rule, based on Matt Kingshotts article: https://itnext.io/laravel-validation-rule-passwords-70364ae0f349
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class StrongPassword implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return preg_match(
"/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@()$%^&*=_{}[\]:;\"'|\\<>,.\/~`±§+-]).{12,30}$/",
$value
);
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return trans('validation.strong_password');
}
}
@dwaard
Copy link
Author

dwaard commented Jun 18, 2020

Do not forget to add a language line to all the resources/lang/xx/validation.php files in your app. Like:

    'strong_password' => 
        'The :attribute must be 12–30 characters, and include a number, a symbol, a lower and a upper case letter',

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment