0
votes

Validation rule:

The password contains characters from at least three of the following five categories:

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

I want to use the validation rule of password like above and my code is like this. But still I can register to my system just by digits only.

 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'min:8', 'regex:/[a-z]/', 'regex:/[A-Z]/', 'regex:/[0-9]/', 'regex:/[@$!%*#?&]/','confirmed'],
1

1 Answers

0
votes

The issue that you have multiple regex rules, one regex can generalize all of yours.

^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$

Which mean, Match:

  • {8,} Minimum eight characters (so there is no need for min:8)
  • A-Za-z at least one letter of any upper and lower
  • @$!%*#?&] at least one special char.
  • \d number/s

so your rules now should look something like this :

'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'regex:^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$','confirmed'],