I am trying to make a password validation using yup where at least 3 of 4 password conditions are met. I am having trouble finding an existing way to do this.
My requirements are this:
At least 8 characters It must use characters from at least three of the following four character types: • English alphabet uppercase letters (A-Z) • English alphabet lowercase letters (a-z) • Numerals (0-9) • Non-alphanumeric symbols (such as !, #, $, %)
And my yup validation (using react-hook-form) is this:
newPassword: yup
.string()
.required('Please enter a password')
.min(8, 'Password too short')
.matches(/^(?=.*[a-z])/, 'Must contain at least one lowercase character')
.matches(/^(?=.*[A-Z])/, 'Must contain at least one uppercase character')
.matches(/^(?=.*[0-9])/, 'Must contain at least one number')
.matches(/^(?=.*[!@#%&])/, 'Must contain at least one special character'),
The problem is of course this makes all 4 conditions required, when only at least 3 of the 4 need to be. Any help on solving this would be appreciated! Thank you