Would it not be wiser to leverage the build-in password validation rules than rolling your own with Regex
Jan 11, 2023
7
Level 1
PHP regex - combination of multiple condition
I have to do validation against the password by the following conditions
- Uppercase and Lowercase
- Numbers
- Special characters
among three any two combination is enough to meet my requirements. It must be achieved by regex.
E.g 'password' => 'required|min:8|regex:/^XXXXX$/',
Level 1
@TwentyHxte Thanks for your answer. But it doesn't meet my requirements so, i made below logic to solve my issue.
$patterns = ['a-z', 'A-Z', '0-9', '`~!@#$%^&*()\-=_+\[\]\{\}\\|;:\'\",.\/<>?'];
$i = 0;
foreach ($patterns as $pattern) {
// Enough to meet any two pattern
if ($i == 2) {
break;
}
if (preg_match('/[' . $pattern . ']/', $password)) {
$i++;
}
}
return ($i == 2) ? true : false;
1 like
Please or to participate in this conversation.