邮箱、手机号、中文 js跟php正则验证

时间:2023-03-08 19:07:42

邮箱正则:

jS:

 var regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
//验证
if(regEmail.test(email)){
alert("success");
}

PHP:

 $regEmail = "/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/";
if(preg_match($regEmail, $email)){
echo 'success';
}

手机号正则:

JS:

 var regPhone = /^1[3|4|5|8][0-9]\d{4,8}$/;
if(regPhone.test(phone)){
alert("success");
}

PHP:

 $regPhone = "/^1[3|4|5|8][0-9]\d{4,8}$/";
if(preg_match($regPhone, $phone)){
echo 'success';
}

中文验证:

JS:

 var regZn = /^[\u4e00-\u9fa5]{2,}$/;
if(regZn.test(zn)){
alert("success");
}

PHP:(详情见:http://blog.sina.com.cn/s/blog_69e1a96d0100vebg.html)

 $regZn = "/^[\x{4e00}-\x{9fa5}]{2,}$/u";
if(preg_match($regZn, $zn)){
echo 'success';
}

如需同时验证字符是否为邮箱或手机号,则可:

/(^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+)|(^1[3|4|5|8][0-9]\d{4,8}$)/