How to check password strength in PHP

1 Answer

0 votes
function check_password_strength($password) {

    $uppercase = preg_match('@[A-Z]@', $password);
    $lowercase = preg_match('@[a-z]@', $password);
    $number = preg_match('@[0-9]@', $password);
    $special_character = preg_match('@[^\w]@', $password);

    if (!$uppercase || !$lowercase || !$number || !$special_character || strlen($password) < 8) {
        echo 'Password should be at least 8 characters length, <br />' .
             ' and include at least one uppercase and lowercase letter, <br />' .
             ' one number, and one special character. <br />';
    } else {
        echo 'Strong password';
    }
}

$password = '12345';
echo $password . "<br />";
check_password_strength($password);
   
echo "<br />";

$password = 'f15e!#q0!';
echo $password . "<br />";
check_password_strength($password);

echo "<br />";

$password = 'f15e!#Q0!';
echo $password . "<br />";
check_password_strength($password);




/*
run:
        
12345
Password should be at least 8 characters length,
and include at least one uppercase and lowercase letter,
one number, and one special character.

f15e!#q0!
Password should be at least 8 characters length,
and include at least one uppercase and lowercase letter,
one number, and one special character.

f15e!#Q0!
Strong password
      
*/

 



answered Sep 20, 2019 by avibootz
...