Verify the syntax of the given name and check if user name is unique or not

Public

With the context of return values we can set the form error.

Get raw version
php
  1. /**
  2.  * Verify the syntax of the given name and check if user name is unique or not
  3.  */
  4. function _mymodule_user_validate_name($name) {
  5. // customized by dscripts for advanced validation
  6. $strlen = strlen($name);
  7. $regx = '/^([a-z]{1})([a-z0-9_-]{5,31})$/';
  8. if (!$strlen) return 0;
  9. if ($strlen < 6) return 1;
  10. if ($strlen > 32) return 2;
  11. if (!preg_match($regx, $name)) return 3;
  12. $user = user_load_by_name($name);
  13. if($user){
  14. return 4;
  15. } else return 5;
  16. }