Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
jQuery validate验证扩展
// 身份证号码验证
jQuery.validator.addMethod("isIdCardNo", function(value, element) {
// var idCard = /^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\w)$/;
return this.optional(element) || isIdCardNo(value);
}, "请输入正确的身份证号码");
// 手机号码验证
jQuery.validator.addMethod("isMobile", function(value, element) {
var length = value.length;
var mobile = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
return this.optional(element) || (length == 11 && mobile.test(value));
}, "请正确填写您的手机号码");
// 长度等于验证
jQuery.validator.addMethod('lengthEq', function(value, element, param){
console.log(this.optional(element) || (value.length >= param));
return this.optional(element) || (value.length >= param);
}, jQuery.validator.format("输入的长度必须等于{0}"));
// 身份证号码的验证规则
function isIdCardNo(num) {
// if (isNaN(num)) {alert("输入的不是数字!"); return false;}
var len = num.length, re;
if (len == 15)
re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{2})(\w)$/);
else if (len == 18)
re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\w)$/);
else {
// alert("输入的数字位数不对。");
return false;
}
var a = num.match(re);
if (a != null) {
if (len == 15) {
var D = new Date("19" + a[3] + "/" + a[4] + "/" + a[5]);
var B = D.getYear() == a[3] && (D.getMonth() + 1) == a[4]
&& D.getDate() == a[5];
} else {
var D = new Date(a[3] + "/" + a[4] + "/" + a[5]);
var B = D.getFullYear() == a[3] && (D.getMonth() + 1) == a[4]
&& D.getDate() == a[5];
}
if (!B) {
// alert("输入的身份证号 "+ a[0] +" 里出生日期不对。");
return false;
}
}
if (!re.test(num)) {
// alert("身份证最后一位只能是数字和字母。");
return false;
}
return true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.