/*
- Username validator
- Ensures a username entered on registration meets specific requirements.
- If it returns 0 that means it passed
- If it returns 1 that means the username contains less than the min characters
- If it returns 2 that means the username contains more than the max characters
- If it returns 3 that means the username contains whitespaces (when not permitted)
- If it returns 4 that means the username contains special characters (when not permitted)
- If it returns 5 that means the username contains numbers (when not permitted)
$username - The username to be validated
$minChars - Minimum amount of characters permitted in the username
$maxChars - Maximum amount of characters permitted in the username
$whiteSpaces - Can the username contain whitespaces?
$specialCharacters - Can the username contain special characters? (eg. !"£$%^)
$numbers - Can the username contain numbers?
*/
public function validateUsername($username, $minChars = 3, $maxChars = 20, $whiteSpaces = false, $specialCharacters = false, $numbers = true) {
$valid = 0;
if (strlen($username) < $minChars)
$valid = 1;
elseif (strlen($username) > $maxChars)
$valid = 2;
elseif (preg_match("/\s/", $username) && !$whiteSpaces)
$valid = 3;
elseif (!preg_match("#^[a-zA-Z0-9]+$#", $username) && !$specialCharacters)
$valid = 4;
elseif (preg_match("([0-9])", $username) && !$numbers)
$valid = 5;
return $valid;
}
Comments