Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
jQuery validate addMethod
var vcity={ 11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",
21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",
33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",
42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",
51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",
63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"
};
isIdCardNo = function(card)
{
//是否为空
if(card === '')
{
return false;
}
//校验长度,类型
if(isCardNo(card) === false)
{
return false;
}
//检查省份
if(checkProvince(card) === false)
{
return false;
}
//校验生日
if(checkBirthday(card) === false)
{
return false;
}
//检验位的检测
if(checkParity(card) === false)
{
return false;
}
return true;
};
//检查号码是否符合规范,包括长度,类型
isCardNo = function(card)
{
//身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X
var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/;
if(reg.test(card) === false)
{
return false;
}
return true;
};
//取身份证前两位,校验省份
checkProvince = function(card)
{
var province = card.substr(0,2);
if(vcity[province] == undefined)
{
return false;
}
return true;
};
//检查生日是否正确
checkBirthday = function(card)
{
var len = card.length;
//身份证15位时,次序为省(3位)市(3位)年(2位)月(2位)日(2位)校验位(3位),皆为数字
if(len == '15')
{
var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/;
var arr_data = card.match(re_fifteen);
var year = arr_data[2];
var month = arr_data[3];
var day = arr_data[4];
var birthday = new Date('19'+year+'/'+month+'/'+day);
return verifyBirthday('19'+year,month,day,birthday);
}
//身份证18位时,次序为省(3位)市(3位)年(4位)月(2位)日(2位)校验位(4位),校验位末尾可能为X
if(len == '18')
{
var re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/;
var arr_data = card.match(re_eighteen);
var year = arr_data[2];
var month = arr_data[3];
var day = arr_data[4];
var birthday = new Date(year+'/'+month+'/'+day);
return verifyBirthday(year,month,day,birthday);
}
return false;
};
//校验日期
verifyBirthday = function(year,month,day,birthday)
{
var now = new Date();
var now_year = now.getFullYear();
//年月日是否合理
if(birthday.getFullYear() == year && (birthday.getMonth() + 1) == month && birthday.getDate() == day)
{
//判断年份的范围(3岁到100岁之间)
var time = now_year - year;
if(time >= 3 && time <= 100)
{
return true;
}
return false;
}
return false;
};
//校验位的检测
checkParity = function(card)
{
//15位转18位
card = changeFivteenToEighteen(card);
var len = card.length;
if(len == '18')
{
var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
var cardTemp = 0, i, valnum;
for(i = 0; i < 17; i ++)
{
cardTemp += card.substr(i, 1) * arrInt[i];
}
valnum = arrCh[cardTemp % 11];
if (valnum == card.substr(17, 1))
{
return true;
}
return false;
}
return false;
};
//15位转18位身份证号
changeFivteenToEighteen = function(card)
{
if(card.length == '15')
{
var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
var cardTemp = 0, i;
card = card.substr(0, 6) + '19' + card.substr(6, card.length - 6);
for(i = 0; i < 17; i ++)
{
cardTemp += card.substr(i, 1) * arrInt[i];
}
card += arrCh[cardTemp % 11];
return card;
}
return card;
};
//只能输入数字和英文逗号(数据校验参数列)
jQuery.validator.addMethod("isCheckColumns", function(value, element) {
var param=/^[\d,]*$/;
return this.optional(element) || (param.test(value) );
}, "多列参数格式:0,1,2,3");
//域名验证
jQuery.validator.addMethod("isDomainName", function(value, element) {
var domainName=/^(((http|https|ftp):\/\/)|())[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/;
return this.optional(element) || (domainName.test(value) );
}, "请正确输入域名如:baidu.com");
//IP地址验证
jQuery.validator.addMethod("isIp", function(value, element) {
var ip = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
return this.optional(element) || (ip.test(value) && (RegExp.$1 < 256 && RegExp.$2 < 256 && RegExp.$3 < 256 && RegExp.$4 < 256));
}, "请正确输入ip如:192.168.1.1");
//用户名验证
jQuery.validator.addMethod("registerName", function(value, element) {
var rename = new RegExp("^[a-zA-Z][a-zA-Z0-9_]{4,15}$", "");
return this.optional(element) || rename.test(value);
}, "只能英文字母开头、可以包括数字和下划线,长度5-16之间");
//字母数字
jQuery.validator.addMethod("alnum", function(value, element) {
return this.optional(element) || /^[a-zA-Z0-9]+$/.test(value);
}, "只能包括英文字母和数字");
// 汉字
jQuery.validator.addMethod("chcharacter", function(value, element) {
var tel = /^[\u4e00-\u9fa5]+$/;
return this.optional(element) || (tel.test(value));
}, "请输入汉字");
//字符验证
jQuery.validator.addMethod("stringCheck", function(value, element) {
return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value);
}, "只能包括中文字、英文字母、数字和下划线");
// 中文字两个字节
jQuery.validator.addMethod("byteRangeLength", function(value, element, param) {
var length = value.length;
for(var i = 0; i < value.length; i++){
if(value.charCodeAt(i) > 127){
length++;
}
}
return this.optional(element) || ( length >= param[0] && length <= param[1] );
}, "请确保输入的值在3-15个字节之间(一个中文字算2个字节)");
// 身份证号码验证
jQuery.validator.addMethod("isIdCardNo", function(value, element) {
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("isTel", function(value, element) {
var tel = /^(0[1-9][0-9]{1,2}\-)+([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/;
//var tel = /^(0[1-9][0-9]{1,2}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/;
//var tel = /^\d{3,4}-?\d{7,9}$/; //电话号码格式010-12345678
return this.optional(element) || (tel.test(value));
}, "请正确填写您的电话号码");
// 联系电话(手机/电话皆可)验证
jQuery.validator.addMethod("isPhone", function(value,element) {
var mobile = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
var tel = /^(0[1-9][0-9]{1,2}\-)+([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/;
return this.optional(element) || (tel.test(value) || mobile.test(value));
}, "请正确填写您的联系电话");
// 邮政编码验证
jQuery.validator.addMethod("isZipCode", function(value, element) {
var tel = /^[0-9]{6}$/;
return this.optional(element) || (tel.test(value));
}, "请正确填写您的邮政编码");
// 验证码验证
jQuery.validator.addMethod("equalToIgnoreCase", function(value, element, param) {
return this.optional(element) || (value.toLowerCase() == $(param).val().toLowerCase());
}, "请输入相同的值");
// QQ号格式验证
jQuery.validator.addMethod("isQQ", function(value,element) {
var qq = /^[1-9]\d{4,9}$/;
return this.optional(element) || (qq.test(value));
}, "请正确填写您的QQ号");
//证件类型验证
function checkType(selectId,textId){
var selectV = $("#"+selectId).find("option:selected").text();
if(selectV.indexOf("身份证")>-1){
$("#"+textId).rules("remove");
$("#"+textId).rules("add", { required: true,isIdCardNo: true });
}else if(selectV.indexOf("军官证")>-1){
$("#"+textId).rules("remove");
$("#"+textId).rules("add", { required: true,stringCheck:true,minlength:6,maxlength:8,messages:{stringCheck:"请输入正确的军官证号"}});
}else if(selectV.indexOf("机动车驾驶证")>-1){
$("#"+textId).rules("remove");
$("#"+textId).rules("add", { required: true,stringCheck:true,maxlength:50,messages:{stringCheck:"请输入正确的机动车驾驶证"}});
}else{
$("#"+textId).rules("remove");
$("#"+textId).rules("add", { required: true,stringCheck:true,maxlength:50,messages:{stringCheck:"请输入正确的证件号"}});
}
$("#"+textId).blur();
}
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.