PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Validation/Validator/Username.php

https://github.com/scottgonzalez/PHP-Input-Validation
PHP | 45 lines | 23 code | 6 blank | 16 comment | 11 complexity | 3403bdd9334156fe56c3268b063b1919 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Username validator
  4. */
  5. /**
  6. * Username validator
  7. */
  8. class Validation_Validator_Username extends Validation_Validator {
  9. /**
  10. * Checks if a value is using valid username syntax
  11. *
  12. * @return boolean true if value is using valid username syntax
  13. */
  14. public function validate() {
  15. $bValid = true;
  16. // don't validate if we don't have a value
  17. if (!$this->hasValue()) {
  18. return true;
  19. }
  20. $sLoweredUsername = strtolower($this->mValue);
  21. // Invalid characters in username.
  22. if (preg_match("/[^a-z0-9_\.@\-]/", $sLoweredUsername)) {
  23. $bValid = false;
  24. }
  25. // Username must contain one alphabetic character.
  26. else if (!(preg_match("/[a-z]/", $sLoweredUsername))) {
  27. $bValid = $bValid && false;
  28. }
  29. // Username must be at least four characters long.
  30. else if (strlen($sLoweredUsername) < 4) {
  31. $bValid = $bValid && false;
  32. }
  33. // Username must be less than fifty characters.
  34. else if (strlen($sLoweredUsername) > 50) {
  35. $bValid = $bValid && false;
  36. }
  37. return $bValid;
  38. }
  39. }