jquery validate自訂規則

來源:互聯網
上載者:User

標籤:pre   $.ajax   inf   身份證   obj   pos   對象   格式   parent   

//檢查社會安全號碼碼是否存在$.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的組件 就無法顯示錯誤資訊了。          }    }});

 

jquery validate自訂規則

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.