Skip to content

Instantly share code, notes, and snippets.

@Michael-Brooks
Last active July 7, 2024 03:44
Show Gist options
  • Save Michael-Brooks/fbbba105cd816ef5c016 to your computer and use it in GitHub Desktop.
Save Michael-Brooks/fbbba105cd816ef5c016 to your computer and use it in GitHub Desktop.
Laravel Password validation Regex (Contain at least one uppercase/lowercase letters and one number)
<?php
/*
* Place this with the rest of your rules.
* Doesn't need to be in an array as there are no pipes.
* Password is required with a minimum of 6 characters
* Should have at least 1 lowercase AND 1 uppercase AND 1 number
*/
$rules = [
'password' => 'required|min:6|regex:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$/'
];
/*
* Use this one if you also require at least one symbol.
* Needs to be in an array as it contains a pipe symbol.
* Password is required with a minimum of 6 characters
* Should have at least 1 lowercase AND 1 uppercase AND 1 number AND 1 symbol
*/
$rules = [
'password' => [
'required',
'min:6',
'regex:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*(_|[^\w])).+$/'
]
];
@rafaelfn
Copy link

rafaelfn commented Jul 1, 2019

How to apply this validation to ResetPassword also?

@rafaelfn

You can override the rules trait by doing something like

    protected function rules()
    {
        return [
            'token' => 'required',
            'email' => 'required|email',
            'password' => [
                'required',
                'min:6',
                'regex:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*(_|[^\w])).+$/'
            ],
        ];
    }

And placing that inside /app/Http/Controllers/Auth/ResetPasswordController.php

Awesome! Worked perfectly here, thank you very much Michael!

@Muniraj132
Copy link

very usefull one..

@sahdevpalaniya
Copy link

only capital latter with a integer and not allowed special characters because i am used for promo codes.

@kas5986
Copy link

kas5986 commented Jan 28, 2024

what about adding custom error messages on laravel 9 and 10 about new rule?

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