正则验证js
(function(global, undefind) {
//正则验证
var verification = {
init: function(that) {
var arr = [];
var thatVal = $(that).val();
var thatIhtml = $(that).parent().siblings().find('i');
arr.push(thatVal, thatIhtml)
//console.log(arrs)
return arr;
},
//为空验证
valEmpty: function(that) {
if(this.init(that)[0].length == 0) {
this.init(that)[1].html('(不能为空)');
} else {
this.init(that)[1].html('');
}
},
//手机号验证
tel_regular: function(that) {
this.valEmpty(that);
if(this.init(that)[0].length != 0) {
var myreg = /^(13[0-9]|15[012356789]|17[0-9]|18[0-9]|14[5678]|19[8-9]|166)[0-9]{8}$/;
if(!myreg.test(this.init(that)[0])) {
this.init(that)[1].html('(请输入正确的手机号)');
} else {
this.init(that)[1].html('');
}
}
},
////姓名验证
name_regular: function(that) {
this.valEmpty(that);
if(this.init(that)[0].length != 0) {
var myreg = /^[a-zA-Z0-9u4e00-u9fa5]{2,500}$/;
if(!myreg.test(this.init(that)[0])) {
this.init(that)[1].html('(姓名格式不正确)');
} else {
this.init(that)[1].html('');
}
}
},
//证件验证
idCard_regular: function(that) {
this.valEmpty(that);
if(this.init(that)[0].length != 0) {
var _p = $(that).parents('p').siblings('.cardType').find('p b').attr('_p');
//console.log(_p)
if(_p != '') {
if(_p == 1) {
if(!this.isIDCard(this.init(that)[0])) {
this.init(that)[1].html('(身份证格式不正确)');
} else {
this.init(that)[1].html('');
}
} else if(_p == 2 || _p == 3) {
var myreg = /^((?!-{2,})[0-9a-zA-Z-]){5,20}$/; //护照
if(!myreg.test(this.init(that)[0])) {
this.init(that)[1].html('(证件格式不正确)');
} else {
this.init(that)[1].html('');
}
//console.log(myreg.test(this.init(that)[0]))
}
} else {
alert('请选择证件类型')
return false;
}
}
},
//发票抬头
Invoices_regular: function(that) {
this.valEmpty(that);
if(this.init(that)[0].length != 0) {
var myreg = /^[0-9a-zA-Z()()u4e00-u9fa5]{0,50}$/;
if(!myreg.test(this.init(that)[0])) {
this.init(that)[1].html('(发票格式不正确)');
} else {
this.init(that)[1].html('');
}
}
},
//纳税人识别号
TaxpayerNum_regular: function(that) {
this.valEmpty(that);
if(this.init(that)[0].length != 0) {
var myreg = /^[0-9a-zA-Z]{15,20}$/;
if(!myreg.test(this.init(that)[0])) {
this.init(that)[1].html('(纳税人识别号格式不正确)');
} else {
this.init(that)[1].html('');
}
}
},
//console.log(isIDCard('142202199306162879'))
////验证身份证
isIDCard: function(str) {
var City = {
11: "u5317u4eac",
12: "u5929u6d25",
13: "u6cb3u5317",
14: "u5c71u897f",
15: "u5185u8499u53e4",
21: "u8fbdu5b81",
22: "u5409u6797",
23: "u9ed1u9f99u6c5f ",
31: "u4e0au6d77",
32: "u6c5fu82cf",
33: "u6d59u6c5f",
34: "u5b89u5fbd",
35: "u798fu5efa",
36: "u6c5fu897f",
37: "u5c71u4e1c",
41: "u6cb3u5357",
42: "u6e56u5317 ",
43: "u6e56u5357",
44: "u5e7fu4e1c",
45: "u5e7fu897f",
46: "u6d77u5357",
50: "u91cdu5e86",
51: "u56dbu5ddd",
52: "u8d35u5dde",
53: "u4e91u5357",
54: "u897fu85cf ",
61: "u9655u897f",
62: "u7518u8083",
63: "u9752u6d77",
64: "u5b81u590f",
65: "u65b0u7586",
71: "u53f0u6e7e",
81: "u9999u6e2f",
82: "u6fb3u95e8",
91: "u56fdu5916 "
};
var iSum = 0;
var info = "";
str = str.replace("uff38", "X");
str = str.replace("x", "X");
if(!/^d{17}(d|x)$/i.test(str)) {
return false;
}
str = str.replace(/x$/i, "a");
if(City[parseInt(str.substr(0, 2))] == null) {
return false;
}
sBirthday = str.substr(6, 4) + "-" + Number(str.substr(10, 2)) + "-" + Number(str.substr(12, 2));
var d = new Date(sBirthday.replace(/-/g, "/"));
if(sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) {
return false;
}
for(var i = 17; i >= 0; i--) {
iSum += (Math.pow(2, i) % 11) * parseInt(str.charAt(17 - i), 11);
}
if(iSum % 11 != 1) {
return false;
}
return true;
}
}
global.verification = verification;
})(window)
<p> <span>姓名<i class="error-red ml5"></i></span> <!--错误信息--> <b><input type="text" placeholder="请填写真实姓名" onblur="verification.name_regular(this)" /></b> <!--调用方法-->
</p>