文章出處

//檢查身份證號碼是否存在
$.validator.addMethod("checkIDCardExist", function (value, element) {
    if ($("#IDType").val() == "1")//為身份證號碼時
    {
        var IDCard = value,
       res = false;
        $.ajax({
            type: "POST",
            async: false,
            url: "/Server/CheckIDCard",
            data: { IDCard: IDCard },
            success: function (response) {
                if (response.isSuccess) {
                    //給form元素賦值
                    res = true;
                    databind(response);
                } else {
                    res = false;
                }
            }
        });
        return res;
    } else {
        return true;
    }

}, "身份證號碼不存在");
//檢查身份證號碼格式
$.validator.addMethod("checkIDCardFormat", function (value, element) {
    if ($("#IDType").val() == "1")//為身份證號碼時
    {
        var msg = ""
          , IDCardObj = {}
          , Birthday = ""
          , Sex = ""
          , Age = "";
        IDCardObj = new IDCard();
        msg = IDCardObj.CheckIdCard(value);
        if (msg != "驗證通過!") {
            $.validator.messages.checkIDCardFormat = msg;
            return false;
        } else {
            //得到生日,性別,年齡
            Birthday = IDCardObj.GetBirthday(value);
            Sex = IDCardObj.GetSex(value);
            Age = IDCardObj.GetAge(value);
            $("#Birthday").val(Birthday);
            $("#Sex").val(Sex == "M" ? "true" : "false");
            $("#form_BasicInfo input[name='Age']").val(Age);
            return true;
        }
    } else {
        return true;
    }
});
//檢查字符數量漢字(算兩個字符)
$.validator.addMethod("checkNumber", function (value, element) {
    var num = 0;  //總個數累加判斷  
    for (var i = 0; i < value.length; i++) {
        //根據charCodeAt來判斷輸入的是中文還是字母,符號    
        var charCode = value.charCodeAt(i);
        if (charCode >= 0 && charCode <= 128) {
            //字符就+1  
            num += 1;
        } else {
            //漢子就+2  
            num += 2;
        }
    };
    if (num > 5) {
        return false;
    } else {
        return true;
    }

}, "<font color='#E47068'>最多可以輸入5個字符,漢字(算兩個)</font>");


$.validator.addMethod("checkDateFormat", function (value, element, params) {
    var reg = /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/;
    if (this.optional(element) || reg.test(value)) {
        if (params[0] == true && reg.test(value)) {//計算年齡
            var Age = new IDCard().GetAgeByDate(value);
            $("input[name='Age']").val(Age);
        }
        return true;
    } else {
        return false;
    }
}, "時間格式有誤");

//定義錯誤提示出現的位置
$.validator.setDefaults({
    errorPlacement: function (error, element) {//error為錯誤提示對象,element為出錯的組件對象  
        if (element.parent(".date").size() > 0) {
            error.appendTo(element.parent().parent());
        } else {
            element.after(error);//默認是加在 輸入框的后面。這個else必須寫。不然其他非radio的組件 就無法顯示錯誤信息了。  
        }
    }
});

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()