邮箱正则:
jS:
1 var regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/; 2 //验证 3 if(regEmail.test(email)){ 4 alert("success"); 5 }
PHP:
1 $regEmail = "/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/"; 2 if(preg_match($regEmail, $email)){ 3 echo 'success'; 4 }
手机号正则:
JS:
1 var regPhone = /^1[3|4|5|8][0-9]d{4,8}$/; 2 if(regPhone.test(phone)){ 3 alert("success"); 4 }
PHP:
1 $regPhone = "/^1[3|4|5|8][0-9]d{4,8}$/"; 2 if(preg_match($regPhone, $phone)){ 3 echo 'success'; 4 }
中文验证:
JS:
1 var regZn = /^[u4e00-u9fa5]{2,}$/; 2 if(regZn.test(zn)){ 3 alert("success"); 4 }
PHP:(详情见:http://blog.sina.com.cn/s/blog_69e1a96d0100vebg.html)
1 $regZn = "/^[x{4e00}-x{9fa5}]{2,}$/u"; 2 if(preg_match($regZn, $zn)){ 3 echo 'success'; 4 }
如需同时验证字符是否为邮箱或手机号,则可:
/(^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+)|(^1[3|4|5|8][0-9]d{4,8}$)/