Home > Backend Development > PHP Tutorial > php checks whether username complies with regulations

php checks whether username complies with regulations

WBOY
Release: 2016-07-25 09:01:13
Original
1402 people have browsed it
php checks whether username complies with regulations http://blog.ddian.cn/?post=869
  1. /**
  2. * Check whether the username meets the regulations (more than two characters, only Chinese, letters, numbers, and underscores)
  3. *
  4. * @param STRING $username The username to be checked
  5. * @return TRUE or FALSE
  6. */
  7. function is_username($username) {
  8. $strlen = strlen($username);
  9. if(!preg_match("/^[a-zA- Z0-9_x7f-xff][a-zA-Z0-9_x7f-xff]+$/", $username)){
  10. return false;
  11. } elseif ( 20 return false;
  12. }
  13. return true;
  14. }
  15. //The following test
  16. $str = 'abc';
  17. if(is_username($str)) {
  18. echo $str.'match';
  19. }else {
  20. echo $str.'does not match';
  21. }
  22. echo '
    ';
  23. $str = 'Chinese';
  24. if(is_username($str)) {
  25. echo $str.'matches';
  26. }else {
  27. echo $str.'does not match';
  28. }
  29. echo '
    ';
  30. $str = '12126_($@';
  31. if(is_username($str)) {
  32. echo $str.'match';
  33. }else {
  34. echo $str.'not match';
  35. }
  36. ?>
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template