Con PHP possiamo verificare i requisiti formali di una password.
La soluzione è la seguente:
function my_check_pwd($password, $requirements = []) {
$errors = [];
if(strlen($password) < $requirements['length']) {
$errors[] = 'At least ' . $requirements['length'] . ' characters.';
}
if($requirements['uppercaselowercase']) {
if (!preg_match('/(?=.*[a-z])(?=.*[A-Z])/', $password)) {
$errors[] = 'Password should include uppercase and lowercase characters.';
}
}
$found_digits = preg_match_all('/\d/', $password, $matches);
if ($found_digits < $requirements['digits']) {
$errors[] = 'Password should include at least ' . $requirements['digits'] . ' digits.';
}
$found_symbols = preg_match_all('/[@%&!*(){}<>\[\]^\?\/|#]/', $password, $matches);
if ($found_symbols < $requirements['symbols']) {
$errors[] = 'Password should include at least ' . $requirements['symbols'] . ' special character.';
}
return $errors;
}