/*
- Password validator
- Ensures a password entered on registration meets specific requirements.
- If it returns 0 that means it passed
- If it returns 1 that means the password has less than the minimum characters
- If it returns 2 that means the password contains less than the minimum special characters
- If it returns 3 that means the password contains less than the minimum numbers
- If it returns 4 that means the password contains less than the minimum capital letters
$password - The password to validate
$minChars - The minimum amount of characters the password is permitted to have
$leastSpecialCharacters - The minimum amount of special characters permitted
$leastNumbers - The minimum amount of numbers permitted
$leastCapitalLetters - The minimum amount of capital letters permitted
*/
public function validatePassword($password, $minChars = 6, $leastSpecialCharacters = 0, $leastNumbers = 0, $leastCapitalLetters = 0) {
$valid = 0;
if (strlen($password) < $minChars)
$valid = 1;
$numSpecialChars = 0;
$numNumbers = 0;
$numCapitalLetters = 0;
// iterate through each character in password
for ($i = 0; $i < strlen($password); $i++) {
$char = substr($password, $i, 1);
if (!preg_match("#^[a-zA-Z0-9]+$#", $char))
$numSpecialChars++;
elseif (preg_match("#^[A-Z]+$#", $char))
$numCapitalLetters++;
elseif (preg_match("#^[0-9]+$#", $char))
$numNumbers++;
}
if ($numSpecialChars < $leastSpecialCharacters)
$valid = 2;
if ($numNumbers < $leastNumbers)
$valid = 3;
if ($numCapitalLetters < $leastCapitalLetters)
$valid = 4;
return $valid;
}
Comments