Advertisement
reenadak

username validator

Feb 20th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.50 KB | None | 0 0
  1.    
  2.     /*
  3.     - Username validator
  4.     - Ensures a username entered on registration meets specific requirements.
  5.     - If it returns 0 that means it passed
  6.     - If it returns 1 that means the username contains less than the min characters
  7.     - If it returns 2 that means the username contains more than the max characters
  8.     - If it returns 3 that means the username contains whitespaces (when not permitted)
  9.     - If it returns 4 that means the username contains special characters (when not permitted)
  10.     - If it returns 5 that means the username contains numbers (when not permitted)
  11.    
  12.     $username          - The username to be validated
  13.     $minChars          - Minimum amount of characters permitted in the username
  14.     $maxChars          - Maximum amount of characters permitted in the username
  15.     $whiteSpaces       - Can the username contain whitespaces?
  16.     $specialCharacters - Can the username contain special characters? (eg. !"£$%^)
  17.     $numbers           - Can the username contain numbers?
  18.     */
  19.    
  20.     public function validateUsername($username, $minChars = 3, $maxChars = 20, $whiteSpaces = false, $specialCharacters = false, $numbers = true) {
  21.         $valid = 0;
  22.        
  23.         if (strlen($username) < $minChars)
  24.             $valid = 1;
  25.         elseif (strlen($username) > $maxChars)
  26.             $valid = 2;
  27.         elseif (preg_match("/\s/", $username) && !$whiteSpaces)
  28.             $valid = 3;
  29.         elseif (!preg_match("#^[a-zA-Z0-9]+$#", $username) && !$specialCharacters)
  30.             $valid = 4;
  31.         elseif (preg_match("([0-9])", $username) && !$numbers)
  32.             $valid = 5;
  33.        
  34.         return $valid;
  35.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement