jquery.validate.extend.src.js 9.45 KB
jQuery.extend(jQuery.validator.messages, {
    required: "必填内容",
    remote: "请勿输入非法字符与非法关键词",
    email: "请输入正确格式的电子邮件",
    url: "请输入合法的网址",
    date: "请输入合法的日期",
    dateISO: "请输入合法的日期 (ISO).",
    number: "请输入合法的数字",
    digits: "只能输入正整数",
    creditcard: "请输入合法的信用卡号",
    equalTo: "请再次输入相同的值",
    accept: "请输入拥有合法后缀名的字符串",
    money: "请输入正确的金额格式",
    maxlength: jQuery.validator.format("请输入一个长度最多是 {0} 的字符串"),
    minlength: jQuery.validator.format("请输入一个长度最少是 {0} 的字符串"),
    rangelength: jQuery.validator.format("请输入一个长度介于 {0} 和 {1} 之间的字符串"),
    range: jQuery.validator.format("请输入一个介于 {0} 和 {1} 之间的值"),
    max: jQuery.validator.format("请输入一个最大为 {0} 的值"),
    min: jQuery.validator.format("请输入一个最小为 {0} 的值"),
    notIllegalCharacter:"请输入正确的内容",
    isPhone:"请输入正确的电话号码",
    isMultiPhone:"请输入正确的电话号码",
    isChar:"请填写正确的中文, 字母或者数字"
});


$.validator.setDefaults({focusInvalid:true, focusCleanup:false });

$.validator.setDefaults({
    errorPlacement: function (error, element) {
       try { 
    	   
            if ( element.parent().is( "span" ) ){
	  	        error.addClass('new-error-placement').appendTo( element.parent().parent() );
            }else {
	  	        error.addClass('new-error-placement').appendTo( element.parent('td') );
            }


        } catch(e) {
           $(element).focus();
        }
    }
});


//增加身份证验证
function isIdCardNo(num) {
    num = num.toUpperCase();
    var factorArr = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1);
    var parityBit = new Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2");
    var varArray = new Array();
    var intValue;
    var lngProduct = 0;
    var intCheckDigit;
    var intStrLen = num.length;
    var idNumber = num;
    // initialize
    if ((intStrLen != 15) && (intStrLen != 18)) {
        return false;
    }
    // check and set value
    for (i = 0; i < intStrLen; i++) {
        varArray[i] = idNumber.charAt(i);
        if ((varArray[i] < '0' || varArray[i] > '9') && (i != 17)) {
            return false;
        } else if (i < 17) {
            varArray[i] = varArray[i] * factorArr[i];
        }
    }
    if (intStrLen == 18) {
        //check date
        var date8 = idNumber.substring(6, 14);
        if (isDate8(date8) == false) {
            return false;
        }
        // calculate the sum of the products
        for (i = 0; i < 17; i++) {
            lngProduct = lngProduct + varArray[i];
        }
        // calculate the check digit
        intCheckDigit = parityBit[lngProduct % 11];
        // check last digit
        if (varArray[17] != intCheckDigit) {
            return false;
        }
    }
    else {        //length is 15
        //check date
        var date6 = idNumber.substring(6, 12);
        if (isDate6(date6) == false) {
            return false;
        }
    }
    return true;
}
function isDate6(sDate) {
    if (!/^[0-9]{6}$/.test(sDate)) {
        return false;
    }
    var myDate = new Date();
    var nowyear = myDate.getFullYear();
    var year, month, day;
    year = sDate.substring(0, 4);
    month = sDate.substring(4, 6);
    
    if (year > nowyear)return false
    if (year < 1700 || year > 2500) return false
    if (month < 1 || month > 12) return false
    return true
}

function isDate8(sDate) {
    if (!/^[0-9]{8}$/.test(sDate)) {
        return false;
    }
    var myDate = new Date();
    var nowyear = myDate.getFullYear();

    var year, month, day;
    year = sDate.substring(0, 4);
    month = sDate.substring(4, 6);
    day = sDate.substring(6, 8);
    var iaMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    
    if (year > nowyear)return false
    if (year < 1700 || year > 2500) return false
    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) iaMonthDays[1] = 29;
    if (month < 1 || month > 12) return false
    if (day < 1 || day > iaMonthDays[month - 1]) return false
    return true
}
//身份证验证
jQuery.validator.addMethod("isIdCardNo", function (value, element) {
    return this.optional(element) || isIdCardNo(value);
}, "请正确输入您的身份证号码");
//电话号码
jQuery.validator.addMethod("isMobile", function (value, element) {
    var length = value.length;
    return this.optional(element) || length == 11 && /^1[3-8]\d{9}$/.test(value);
}, "请填写正确的手机号码");

//短信验证码
jQuery.validator.addMethod("isVerifCode", function (value, element) {
    var length = value.length;
    return this.optional(element) || length == 6 && /^\d{6}$/.test(value);
}, "请填写正确的验证码");
//验证是否是企业
jQuery.validator.addMethod("isCompany", function (value, element) {
    var length = value.length;
    return this.optional(element) || /^[\u4E00-\u9FA5]+$/.test(value);
}, "请填写正确的企业名称");

/**
 * 验证是中文, 字母, 数字
 */
jQuery.validator.addMethod("isChar", function (value, element) {
    var length = value.length;
    return this.optional(element) || /^[\u4E00-\u9FA50-9a-zA-Z]+$/.test(value);
}, "请填写正确的中文, 字母或者数字!");

jQuery.validator.addMethod("isConcat", function (value, element) {
    var length = value.length;
    return this.optional(element) || /^[\u4E00-\u9FA50-9a-zA-Z\-]+$/.test(value);
}, "输入中含有非法字符!");

//验证真实姓名
jQuery.validator.addMethod("isRealName", function (value, element) {
    //var length = value.length;
    return this.optional(element) || /^[\u4E00-\u9FA5+·]{2,10}$/.test(value) && !/^[\u4E00-\u9FA5+·]{2,10}[·]$/.test(value) && !/^[·][\u4E00-\u9FA5+.]{2,10}$/.test(value)
}, "请填写正确的名字");

//验证钱
jQuery.validator.addMethod("money", function (value, element) {
    var money = /^([1-9][0-9]+|[0-9])(\.\d{1,2})?$/
    return this.optional(element) || money.test(value);
}, "请输入正确的金额");

//验证电话号码
jQuery.validator.addMethod("isPhone", function (value, element) {
    var phone = /((^\d{11}$)|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/
    return this.optional(element) || phone.test(value);
}, "请输入正确的电话号码");


//验证多个电话号码
jQuery.validator.addMethod("isMultiPhone", function (value, element) {
    var phone = /^(((\d{11})|((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})))([;、;,、,]?))+$/
    return this.optional(element) || phone.test(value);
}, "请输入正确的电话号码");

// 联系电话号码验证
jQuery.validator.addMethod("isPlane", function(value, element) {
    var tel = /^(\d{3,4}-)\d{7,9}$/g;
    return this.optional(element) || (tel.test(value));
}, "请正确填写您的联系电话。");
// 联系电话(手机/皆可)验证
jQuery.validator.addMethod("isTel", function(value,element) {
    var length = value.length;
    var mobile = /^1[3-8]\d{9}$/ ;
    
    var tel = /^(\d{3,4}-)\d{7,9}$/g;
    return this.optional(element) || tel.test(value) || (length==11 && mobile.test(value));
}, "请正确填写您的联系方式");



//不是非法字符
jQuery.validator.addMethod("notIllegalCharacter", function (value, element) {
    var char = /^[^><\/\\;&]*$/
    return this.optional(element) || char.test(value);
}, "请输入正确的内容");

//验证是否是工商注册号码
jQuery.validator.addMethod("isBusinessReg", function (value, element) {

    return this.optional(element) || reg(value);
}, "请填写正确的工商注册号码");
function reg(value) {
  //只获取13位或者15位的工商号
  var len = value.length;

  switch(len){
    case 13:
      return /[0-9a-zA-Z]{13}/.test(value);
    case 15:
      return /[0-9a-zA-Z]{15}/.test(value);
    case 18:
      return /[0-9a-zA-Z]{18}/.test(value);
    default:
      return false;
  };
}

jQuery.validator.addMethod("diliMethod", function(value, element, param){
    var me = param;
//    var t = eval(me + "('"+value+"',"+element.id+")");
    var t = window[me].call(this, value, element);

    if(t != true) {
        $.validator.messages["diliMethod"] = t;
        return false;
    }
    return true;
});


jQuery.validator.addMethod("diliPattern", function(value, element, param){
    var params = param.split("/,");
    var pa = eval(params[0]+"/");
    var msg = params[1];
    var t = pa.test(value);
    if(t == false) {
        $.validator.messages["diliPattern"] = msg !== undefined ? msg : $.validator.messages[msg];
    }
    return t;
});

jQuery.validator.addMethod("isCarNo", function (value, element) {
    var carNO = /^[\u4E00-\u9FA5]{1}[a-zA-Z]{1}[\da-zA-Z]{5}$/;
    return this.optional(element) || carNO.test(value);
}, "请输入正确的车牌号码!");

jQuery.validator.addMethod("isAddr", function (value, element) {
    var reg = /[\#\<\>\=\'\"]+/g;
    return this.optional(element) || !reg.test(value);
}, "请输入正确的地址信息!");

//过滤字符
jQuery.validator.addMethod("filterCharacter", function (value, element) {
    var char = /^[0-9a-zA-Z\u4e00-\u9fa5/()()&《》【】\+=\]\[]+$/g;
    return this.optional(element) || char.test(value);
}, "请输入正确的内容");