吾生也有涯,而知也无涯~欢迎优化补充、指正!
(直接调用方法稍加修改即可使用,或者单独粘贴相应验证!)
* 手机号码验证包括最新16、19字段的手机号
*本文是在tp5下进行编译与调试的
/*
*验证用户名,手机号,邮箱,QQ,微信号,身份证
*(用户名:字母、数字或下划线组成大于等于2位的字符串)
*/
public function verify($username='',$mobile='',$email='',$qq='',$wx='')
{
//用户名验证
if(!empty($username)&&!preg_match('/^[a-zA-Z0-9_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+$/',$username))
return ['code'=>'201','msg'=>'用户名格式不正确!'];
//验证电话
if(!empty($mobile)&&!preg_match("/^1[3456789]\d{9}$/ims",$mobile))
return ['code'=>'201','msg'=>'请输入正确的手机号码!'];
//验证邮箱
if(!empty($email)&&!preg_match("/^[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*@([a-zA-Z0-9]+[-.])+([a-z]{2,5})$/ims",$email))
return ['code'=>'201','msg'=>'请输入正确的邮箱!'];
//验证qq
if(!empty($qq)&&!preg_match("/^\d{5,10}$/isu",$qq))
return ['code'=>'201','msg'=>'请输入正确的QQ号码!'];
//验证微信号
if(!empty($wx)&&!preg_match("/^[_a-zA-Z0-9]{5,19}+$/isu",$wx))
return ['code'=>'201','msg'=>'请输入正确的微信号!'];
//身份证验证
if(!$this->verifyCard($userCard))
return ['code'=>'201','msg'=>'请输入正确的身份证号!'];
/*
*数字串验证
* 正则:/^\d*$/
* 函数:is_numeric (小数也会认为是数字,而前面正则会把小数点当作字符)
*/
return '非空该方法没进行验证哦!';
}
/**
* 判断是否为合法的身份证号码
* @param $vStr
* @return boolen
*/
public function verifyCard($vStr='')
{
$vCity = array('11','12','13','14','15','21','22','23','31','32','33','34','35','36','37','41','42','43','44','45','46','50','51','52','53','54','61','62','63','64','65','71','81','82','91');
if (!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/', $vStr))
return false;
if (!in_array(substr($vStr, 0, 2), $vCity))
return false;
$vStr = preg_replace('/[xX]$/i', 'a', $vStr);
$vLength = strlen($vStr);
if ($vLength == 18) {
$vBirthday = substr($vStr, 6, 4) . '-' . substr($vStr, 10, 2) . '-' . substr($vStr, 12, 2);
} else {
$vBirthday = '19' . substr($vStr, 6, 2) . '-' . substr($vStr, 8, 2) . '-' . substr($vStr, 10, 2);
}
if (date('Y-m-d', strtotime($vBirthday)) != $vBirthday)
return false;
if ($vLength == 18) {
$vSum = 0;
for ($i = 17 ; $i >= 0 ; $i--) {
$vSubStr = substr($vStr, 17 - $i, 1);
$vSum += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr ,11));
}
if($vSum % 11 != 1)
return false;
}
return true;
}