Ad

Password Validation In Laravel

- 1 answer

I need password validation like

1 English uppercase characters (A – Z)
2 English lowercase characters (a – z)
3 Base 10 digits (0 – 9)
4 Non-alphanumeric (For example: !, $, #, or %)
5 Unicode characters

I have applied some code but result I am getting is an error. I think regex I have used in wrong format. Please check my code and correct me. Thanks

AuthController.php

protected function validator(array $data)
    {
        return Validator::make($data, [
            'firstname' => 'required|max:255',
            'lastname' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|
                           regex:array("/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\d\X])(?=.*[!$#%]).*$/")|
                           min:8|
                           confirmed',
        ]);
    }
Ad

Answer

Regex validator takes regex string, but you pass as an array.

Try to change your code

regex:array("/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\d\X])(?=.*[!$#%]).*$/")

to

regex:/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\d\X])(?=.*[!$#%]).*$/

Also, look at @vks answer. His regex looks more clear.

P.S. When using the regex pattern, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character.

If the pipe (|) sigh is available in your regular expression pattern it could conflict with the separator.

Ad
source: stackoverflow.com
Ad