正则验证js

正则验证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-9\u4e00-\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: "\u5317\u4eac",
				12: "\u5929\u6d25",
				13: "\u6cb3\u5317",
				14: "\u5c71\u897f",
				15: "\u5185\u8499\u53e4",
				21: "\u8fbd\u5b81",
				22: "\u5409\u6797",
				23: "\u9ed1\u9f99\u6c5f ",
				31: "\u4e0a\u6d77",
				32: "\u6c5f\u82cf",
				33: "\u6d59\u6c5f",
				34: "\u5b89\u5fbd",
				35: "\u798f\u5efa",
				36: "\u6c5f\u897f",
				37: "\u5c71\u4e1c",
				41: "\u6cb3\u5357",
				42: "\u6e56\u5317 ",
				43: "\u6e56\u5357",
				44: "\u5e7f\u4e1c",
				45: "\u5e7f\u897f",
				46: "\u6d77\u5357",
				50: "\u91cd\u5e86",
				51: "\u56db\u5ddd",
				52: "\u8d35\u5dde",
				53: "\u4e91\u5357",
				54: "\u897f\u85cf ",
				61: "\u9655\u897f",
				62: "\u7518\u8083",
				63: "\u9752\u6d77",
				64: "\u5b81\u590f",
				65: "\u65b0\u7586",
				71: "\u53f0\u6e7e",
				81: "\u9999\u6e2f",
				82: "\u6fb3\u95e8",
				91: "\u56fd\u5916 "
			};
			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>

  

posted @ 2018-07-19 16:09  小囧光  阅读(1596)  评论(0编辑  收藏  举报