1public function checkPassword($pwd, &$errors) {
2 $errors_init = $errors;
3
4 if (strlen($pwd) < 8) {
5 $errors[] = "Password too short!";
6 }
7
8 if (!preg_match("#[0-9]+#", $pwd)) {
9 $errors[] = "Password must include at least one number!";
10 }
11
12 if (!preg_match("#[a-zA-Z]+#", $pwd)) {
13 $errors[] = "Password must include at least one letter!";
14 }
15
16 return ($errors == $errors_init);
17}
1// Given password
2$password = 'user-input-pass';
3
4// Validate password strength
5$uppercase = preg_match('@[A-Z]@', $password);
6$lowercase = preg_match('@[a-z]@', $password);
7$number = preg_match('@[0-9]@', $password);
8$specialChars = preg_match('@[^\w]@', $password);
9
10if(!$uppercase || !$lowercase || !$number || !$specialChars || strlen($password) < 8) {
11 echo 'Password should be at least 8 characters in length and should include at least one upper case letter, one number, and one special character.';
12}else{
13 echo 'Strong password.';
14}