Laravel password validation rule

Laravel password validation rule

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Crafting Robust Password Validation in Laravel

Building a registration form is trivial if you only check that a password field isn't empty. However, security best practices dictate that we enforce complexity rules to prevent users from choosing easily guessable strings like "123456" or "password". In Laravel, the Validator class provides a robust set of tools for this, but standard built-in rules often fall short of modern requirements.

The specific requirement here is complex: the password must contain characters from at least three of five distinct categories. This includes uppercase letters (A–Z), lowercase letters (a–z), digits (0–9), non-alphanumeric symbols (!, $, #, %), and Unicode characters. Achieving this requires moving beyond simple string matching and utilizing Laravel's custom validation rules.

Defining the Complexity Logic

Laravel does not ship with a single rule that checks "three out of five categories." While you can chain existing rules like min:6 or regex, checking character category diversity is best handled by a custom validation rule. This approach keeps your logic isolated, testable, and independent of the specific form fields.

You can define this custom rule directly in your controller or service class using the Validator::extend method (in Laravel 5) or defining a custom rule class (preferred in Laravel 6+). For this scenario, we will demonstrate the extension method as it is often quicker to implement for one-off requirements.

Implementing the Custom Rule

First, create a closure that defines the validation logic. This function receives the attribute value and a data bag containing all available values from the request.

Validator::extend('password_complexity', function ($attribute, $value, $parameters, $validator) {
    if (strlen($value) < 8) {
        return false;
    }

    // Define the five categories
    $categories = [
        'upper'      => preg_match('/[A-Z]/', $value),
        'lower'      => preg_match('/[a-z]/', $value),
        'digit'      => preg_match('/[0-9]/', $value),
        'symbol'     => preg_match('/[^a-zA-Z0-9]/', $value),
        'unicode'    => preg_match('/[\x{200}-\x{7FF}\x{800}-\x{FFFF}]/u', $value) // Simplified Unicode check logic
    ];

    // Count how many categories are met
    $count = array_sum(array_filter($categories));

    return $count >= 3;
});

Note: The specific regex for Unicode characters can be tricky depending on the PCRE version. Often, developers use a simpler heuristic, such as checking for non-ASCII bytes, but the logic above illustrates the category counting mechanism.

Applying the Rule to Your Validator

Once the rule is registered, you can apply it just like any standard validation rule in your controller. Replace the generic 'required' check with your new password_complexity rule.

use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request; // Assuming using Facades or Request class

// Define the rules
$rules = [
    'name' => 'required',
    'email' => 'required|email|unique:ducks',
    'password' => 'required|min:8|password_complexity',
    'password_confirm' => 'required|same:password'
];

// Instantiate the validator
$validator = Validator::make(Request::all(), $rules);

// Handle validation failure
if ($validator->fails()) {
    $messages = $validator->messages();

    // Redirect with errors
    return redirect('home')
        ->withErrors($validator)
        ->withInput();
}

Handling Validation Errors

When a user fails this check, the error message needs to be clear. By default, Laravel will output a generic "Validation." error unless you explicitly define the failure message in your Validator::extend closure or pass a custom array of messages.

You can define a helpful message directly in the extension:

Validator::extend('password_complexity', function ($attribute, $value, $parameters, $validator) {
    // ... logic from before ...
    
    $message = 'The :attribute must contain at least three of the following categories: uppercase, lowercase, numbers, symbols, or unicode.';
    
    return false; 
});

// Then pass the message explicitly if you modify the logic inside to return false
Validator::extend('password_complexity', function ($attribute, $value, $parameters, $validator) {
    // ... logic ...
    return $count >= 3;
}, 'The :attribute must contain characters from at least three of the following five categories.');

Trade-offs and Considerations

While custom rules are powerful, they introduce a coupling between your validation logic and your application. If you decide later to change the complexity requirements (e.g., adding a requirement for special characters only), you have to update both the rule definition and any documentation or frontend error messages.

Another consideration is performance. Custom validation rules run in PHP after the request is processed, so they add a slight overhead compared to database checks or simple regex on standard fields. However, for password registration forms, this overhead is negligible compared to the security benefit of enforcing strong passwords.

Finally, ensure that your frontend form provides immediate feedback if possible. Relying solely on server-side validation redirects can lead to a poor user experience where users only realize their password is invalid after submitting the entire form. Combining client-side hints (like showing which categories are missing) with this robust Laravel backend rule creates a secure and responsive application.

Our network