Skip to content

Instantly share code, notes, and snippets.

@avramovic
Last active March 24, 2021 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avramovic/f6a8c9ae23f8292aa0376086c8378353 to your computer and use it in GitHub Desktop.
Save avramovic/f6a8c9ae23f8292aa0376086c8378353 to your computer and use it in GitHub Desktop.
Laravel 5+ password validation rules
<?php
namespace App\Validators;
class PasswordValidator
{
public function validateLetters($attribute, $value)
{
return preg_match('/\pL/', $value);
}
public function validateNumbers($attribute, $value)
{
return preg_match('/\pN/', $value);
}
public function validateCaseDiff($attribute, $value)
{
return preg_match('/(\p{Ll}+.*\p{Lu})|(\p{Lu}+.*\p{Ll})/u', $value);
}
public function validateSymbols($attribute, $value)
{
return preg_match('/\p{Z}|\p{S}|\p{P}/', $value);
}
}
<?php
return [
// ... add these to your validation translation file(s)
"letters" => "The :attribute must include at least one letter.",
"case_diff" => "The :attribute must include both upper and lower case letters.",
"numbers" => "The :attribute must include at least one number.",
"symbols" => "The :attribute must include at least one symbol.",
];
<?php
namespace App\Providers;
use App;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
class ValidationServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Validator::extend('case_diff', 'App\Validators\PasswordValidator@validateCaseDiff');
Validator::extend('numbers', 'App\Validators\PasswordValidator@validateNumbers');
Validator::extend('letters', 'App\Validators\PasswordValidator@validateLetters');
Validator::extend('symbols', 'App\Validators\PasswordValidator@validateSymbols');
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment