password strength php

Solutions on MaxInterview for password strength php by the best coders in the world

showing results for - "password strength php"
Aitana
20 Apr 2018
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}
Johann
24 Jun 2017
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}