php用户注册ID验证正则表达式
下面这个正则验证用户名的方法原则是这样的用户名必须是由字母带数字带定划线组成了,下面一起来看看例子吧。
1.检查用户名是否符合规定 两位以上的字母,数字,或者下划线
<?php
/**
* 检查用户名是否符合规定
*
* @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)) {
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 = '/^[\\~!@#$%^&*()-_=+|{}\[\],.?\/:;\'\"\d\w]{' . $minLen . ',' . $maxLen . '}$/';
$v = trim($value);
if (empty($v)) return false;
return preg_match($match, $v);
}3、email验证
function isEmail($value, $match = '/^[\w\d]+[\w\d-.]*@[\w\d-.]+\.[\w\d]{2,10}$/i') {
$v = trim($value);
if (empty($v)) return false;
return preg_match($match, $v);
}注意,把内容中的\替换成小写的\就可以正常使用了,因为本服务器自动过滤\所以本文替换成全角了。
本文地址:http://www.phprm.com/code/67315.html
转载随意,但请附上文章地址:-)