Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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])).+$/'
]
];
@wdmtech

This comment has been minimized.

Copy link

wdmtech commented Jul 24, 2015

Saved me some time, thanks!

@wdmtech

This comment has been minimized.

Copy link

wdmtech commented Jul 28, 2015

^(?=.*[a-z|A-Z])(?=.*[A-Z])(?=.*\d).+$
added extra OR A-Z as wasn't matching numeric passwords w/out lowercase chars

@HashmatWaziri

This comment has been minimized.

Copy link

HashmatWaziri commented Dec 24, 2015

thanks. it worked

@eddnav

This comment has been minimized.

Copy link

eddnav commented Mar 29, 2016

Thanks, saved some time with this. Tested it with RegExr.com and it's what I needed.

@llevvi

This comment has been minimized.

Copy link

llevvi commented Jun 26, 2016

Thanks! I changed mine to accept lowercase or uppercase letters:
/^(?=.*[a-zA-Z])(?=.*\d).+$/

@ghost

This comment has been minimized.

Copy link

ghost commented Jul 26, 2016

Thanks!

@hariharan2684

This comment has been minimized.

Copy link

hariharan2684 commented Aug 8, 2016

Thank you very much

@sushantaryal

This comment has been minimized.

Copy link

sushantaryal commented Sep 14, 2016

Thank You

@rosshristov

This comment has been minimized.

Copy link

rosshristov commented Nov 14, 2016

Thank you

@findela

This comment has been minimized.

Copy link

findela commented May 11, 2017

Thanks.

@anmolio

This comment has been minimized.

Copy link

anmolio commented May 16, 2017

Great ! Easy and crisp 🍡

@ankurk91

This comment has been minimized.

Copy link

ankurk91 commented Aug 25, 2017

Adding some more goodies -

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Validator;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // Register custom validators
        Validator::extend('current_password', function ($attribute, $value, $parameters, $validator) {
            return \Illuminate\Support\Facades\Hash::check($value, auth()->user()->password);
        }, "The :attribute field does not match with your current password.");

        Validator::extend('strong_password', function ($attribute, $value, $parameters, $validator) {
            // Contain at least one uppercase/lowercase letters, one number and one special char
            return preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*(_|[^\w])).+$/', (string)$value);
        }, 'Please make a strong password');

    }
}
@dneey

This comment has been minimized.

Copy link

dneey commented Mar 2, 2018

Thanks

@rafaelfn

This comment has been minimized.

Copy link

rafaelfn commented Jun 30, 2019

How to apply this validation to ResetPassword also?

@Michael-Brooks

This comment has been minimized.

Copy link
Owner Author

Michael-Brooks 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

@rafaelfn

This comment has been minimized.

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.