jquery Validate Custom Rules

Source: Internet
Author: User

//Check if the ID number exists$.validator.addmethod ("Checkidcardexist",function(value, Element) {if($ ("#IDType"). val () = = "1")//when the ID number    {        varIdcard =value, res=false; $.ajax ({type:"POST", Async:false, URL:"/server/checkidcard", data: {Idcard:idcard}, Success:function(response) {if(response.issuccess) {//Assigning a value to a FORM elementres =true;                DataBind (response); } Else{res=false;        }            }        }); returnRes; } Else {        return true; }}, "ID number does not exist");//Check the ID number format$.validator.addmethod ("Checkidcardformat",function(value, Element) {if($ ("#IDType"). val () = = "1")//when the ID number    {        varmsg = "", Idcardobj={}, Birthday= "", Sex= "", Age= ""; Idcardobj=NewIdcard (); Msg=Idcardobj.checkidcard (value); if(msg! = "Verify through!")) {$.validator.messages.checkidcardformat=msg; return false; } Else {            //get birthday, sex, ageBirthday =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; }});//Check the number of characters kanji (count two characters)$.validator.addmethod ("CheckNumber",function(value, Element) {varnum = 0;//Total number summation judgment     for(vari = 0; i < value.length; i++) {        //according to charCodeAt to determine whether the input is Chinese or letter, symbol        varCharCode =value.charcodeat (i); if(charcode >= 0 && charcode <= 128) {            //The character is +1 .num + = 1; } Else {            //Man is +2 .num + = 2;    }    }; if(Num > 5) {        return false; } Else {        return true; }}, "<font color= ' #E47068 ' > can enter up to 5 characters, kanji (count two) </font>"); $.validator.addmethod ("Checkdateformat",function(value, element, params) {varreg =/^ (?:(?! 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)) {//Calculate Age            varAge =NewIdcard ().            Getagebydate (value); $("Input[name= ' age ']"). Val (age); }        return true; } Else {        return false; }}, "Time format is incorrect");//define where the error prompt appears$.validator.setdefaults ({errorplacement:function(Error, Element) {//error is the wrong prompt object, element is an error component Object        if(Element.parent (". Date"). Size () > 0) {error.appendto (Element.parent (). parent ()); } Else{element.after (error);//The default is added after the input box. This else must be written.  Otherwise, other non-radio components will not be able to display the error message.         }    }});

jquery Validate Custom Rules

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.