I liked the answer of r3bel, so I had a game with it, and as a password verification function, I got the following:
function password_strength_check($password, $min_len = 8, $max_len = 70, $req_digit = 1, $req_lower = 1, $req_upper = 1, $req_symbol = 1) { // Build regex string depending on requirements for the password $regex = '/^'; if ($req_digit == 1) { $regex .= '(?=.*\d)'; } // Match at least 1 digit if ($req_lower == 1) { $regex .= '(?=.*[az])'; } // Match at least 1 lowercase letter if ($req_upper == 1) { $regex .= '(?=.*[AZ])'; } // Match at least 1 uppercase letter if ($req_symbol == 1) { $regex .= '(?=.*[^a-zA-Z\d])'; } // Match at least 1 character that is none of the above $regex .= '.{' . $min_len . ',' . $max_len . '}$/'; if(preg_match($regex, $password)) { return TRUE; } else { return FALSE; } }
The maximum / minimum length is by default or configurable, each requirement is enabled by default, but can be disabled, and I would like to support any characters, so the last requirement is "all that is not one of the above types", but a fixed character set.
David bell
source share