[Reprint] [jquery. validate] the custom method implements logic verification of "mobile phone number or fixed phone number", jqueryvalidate
In recent project development, the following requirement is met: "Enter at least one mobile phone number or fixed phone number", as shown in:
The jquery. validate. js verification component used by the project. Currently, the component does not support the "or" logic verification, So you define
JQuery. validator. addMethod ("phone", function (value, element) {var mobile =$ ("# mobile "). val (); // mobile phone number var telephone =$ ("# telephone "). val (); // fixed telephone var assumerule =/^ (13 [0-9] | 14 [5 | 7] | 15 [0 | 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9] | 18 [0-9] | 170) \ d {8 }$/; var telephoneRule =/^ \ d {3, 4 }-? \ D {7, 9} $/; // if (isEmpty (mobile) & isEmpty (telephone) {// custom error prompt $ ("# receivingMobile_tip "). addClass ("errorHint "). text ("enter a landline or mobile phone number"); return false;} var mobilePass = false; var telephonePass = false; // if (! IsEmpty (mobile) & isEmpty (telephone) {if (! Assumerule. test (mobile) {// custom error code $ ("# ingingmobilephone_tip "). removeClass ("successHint "). addClass ("errorHint "). text ("Incorrect mobile Phone Number Format"); return false;} else {mobilePass = true ;}// if (isEmpty (mobile )&&! IsEmpty (telephone) {if (! TelephoneRule. test (telephone) {// custom error code $ ("# receivingTelephone_tip "). removeClass ("successHint "). addClass ("errorHint "). text ("Incorrect landline format"); return false;} else {telephonePass = true ;}} if (mobilePass | telephonePass) {// custom success prompt $ ("# receivingTelephone_tip "). removeClass ("errorHint "). addClass ("successHint "). text (''); return true;} else {return false ;}}," ignore ");
Supplement the isEmpty function:
// Null String judge function isEmpty (v, allowBlank) {return v = null | v = undefined | (! AllowBlank? V = "": false );}
Process the errorPlacement of validate:
ErrorPlacement: function (error, element) {// if (error. text () = "ignore") {return ;}
}
Use in rules
rules : { telephone : { phone : [] }, mobile : { phone : [] } }