Home  >  Article  >  Backend Development  >  php regular expression to verify username

php regular expression to verify username

墨辰丷
墨辰丷Original
2018-06-06 10:21:401579browse

本篇文章主要介绍php正则表达式验证用户名,感兴趣的朋友参考下,希望对大家有所帮助。

1.检查用户名是否符合规定“两位以上的字母,数字,或者下划线”,代码如下:

/** 
 * 检查用户名是否符合规定 
 * 
 * @param STRING $username 要检查的用户名 
 * @return TRUE or FALSE 
 */ 
function is_username($username) 
{ 
$strlen = strlen($username); 
if (!preg_match("/^[a-zA-Z0-9_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+$/", 
$username)) //开源软件:phpfensi.com 
{ 
return false; 
} elseif (20 < $strlen || $strlen < 2) 
{ 
return false; 
} 
return true; 
}

两位以上的字母,数字,或者下划线:^[a-zA-Z0-9_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+$.

注:在这里,字母是a-z,A-Z,以及从127 到255(0x7f-0xff)的 ASCII 字符

2、密码:6—20位,由字母、数字组成,代码如下:

function isPWD($value,$minLen=5,$maxLen=16){ 
$match=&#39;/^[\\~!@#$%^&*()-_=+|{}\[\],.?\/:;\&#39;\"\d\w]{&#39;.$minLen.&#39;,&#39;.$maxLen.&#39;}$/&#39;; 
$v = trim($value); 
if(emptyempty($v)) 
return false; 
return preg_match($match,$v); 
}

3、email验证,代码如下:

function isEmail($value,$match=&#39;/^[\w\d]+[\wd-.]*@[w\d-.]+\.[\w\d]{2,10}$/i&#39;)

{ 
$v = trim($value); 
if(emptyempty($v)) 
return false; 
return preg_match($match,$v); 
}

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

PHP基于邮件类发送邮件的方法

php提交post数组参数的用法

php中常见的排序算法

The above is the detailed content of php regular expression to verify username. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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