Home > Backend Development > PHP Tutorial > php regular expression to verify username

php regular expression to verify username

墨辰丷
Release: 2023-03-29 22:36:02
Original
4364 people have browsed it

本篇文章主要介绍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; 
}
Copy after login

两位以上的字母,数字,或者下划线:^[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); 
}
Copy after login

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); 
}
Copy after login

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

相关推荐:

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!

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