本文最后更新于2019-10-22,已超过 1年没有更新,如果文章内容、图片或者下载资源失效,请留言反馈,我会及时处理,谢谢!
温馨提示:本文共1650个字,读完预计5分钟。
文章目录
PHP版
$check = '/^1[3456789]\d{9}$/';
if(!preg_match($check, $mobile)){
$res[‘res_code’] = 2;
$res[‘res_msg’] = '手机号填写有误';
return json($res);
}
js版
手机号码正则表达式验证
function checkPhone(){
var phone = document.getElementById('phone').value;
if(!(/^1[3456789]\d{9}$/.test(phone))){
alert("手机号码有误,请重填");
return false;
}
}
或者
function checkPhone(){
var phone = document.getElementById('phone').value;
if(!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone))){
alert("手机号码有误,请重填");
return false;
}
}
固定电话号码正则表达式
function checkTel(){
var tel = document.getElementById('tel').value;
if(!/^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/.test(tel)){
alert('固定电话有误,请重填');
return false;
}
}
身份证正则
//身份证正则表达式(15位)
isIDCard1=/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;
//身份证正则表达式(18位)
isIDCard2=/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/;
身份证正则合并:(^\d{15}$)|(^\d{17}([0-9]|X)$)
其他
提取信息中的网络链接:(h|H)(r|R)(e|E)(f|F) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)?
提取信息中的邮件地址:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
提取信息中的图片链接:(s|S)(r|R)(c|C) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)?
提取信息中的IP地址:(\d+)\.(\d+)\.(\d+)\.(\d+)
提取信息中的中国电话号码(包括移动和固定电话):(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}
提取信息中的中国邮政编码:[1-9]{1}(\d+){5}
提取信息中的中国身份证号码:\d{18}|\d{15}
提取信息中的整数:\d+
提取信息中的浮点数(即小数):(-?\d*)\.?\d+
提取信息中的任何数字 :(-?\d*)(\.\d+)?
提取信息中的中文字符串:[\u4e00-\u9fa5]*
提取信息中的双字节字符串 (汉字):[^\x00-\xff]*
使用
test()方法在字符串中查找是否存在指定的正则表达式,并返回布尔值,如果存在则返回true,否则返回false。
var pattern = new RegExp('Box','i');
var str = 'box';
alert(pattern.test(str)); //true
var pattern = /Box/i;
var str = 'box';
alert(pattern.test(str)); //true
var pattern = /Box/i;
var str = 'This is a box';
alert(pattern.test(str)); //true