Recent project development encountered such a demand "mobile phone number or fixed at least one", as shown in the following figure:
The Jquery.validate.js validation component of the project, which currently does not support this "or" logical validation, defines a
JQuery.validator.addMethod ("Phone", function (value, Element) {var mobile = $ ("#mobile"). Val ()/mobile number Var Te Lephone = $ ("#telephone"). Val ()//fixed telephone var mobilerule =/^ (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}$/; None is filled in if (IsEmpty (mobile) && IsEmpty (telephone)) {//Custom error hint $ ("#receivingMobile_tip"). ADDC
Lass ("Errorhint"). Text ("Please fill in the fixed telephone or mobile phone number");
return false;
var mobilepass = false;
var telephonepass = false;
The mobile phone is filled in, the fixed telephone does not fill in if (!isempty (mobile) && IsEmpty (telephone)) {if (!mobilerule.test (mobile)) {
Custom error hint $ ("#receivingMobilePhone_tip"). Removeclass ("Successhint"). AddClass ("Errorhint"). Text ("Phone number format is not correct");
return false;
else {Mobilepass = true; }//Phone not filled, fixed telephone filled in if (IsEmpty (mobile) &&!isempty (telephone)) {if (!telephoneruLe.test (telephone)) {//Custom error hint $ ("#receivingTelephone_tip"). Removeclass ("Successhint"). AddClass ("Erro
Rhint "). Text (" Fixed telephone format is not correct ");
return false;
else {Telephonepass = true; } if (Mobilepass | | telephonepass) {//Custom success Hint $ ("#receivingTelephone_tip"). Removeclass ("ER
Rorhint "). AddClass (" Successhint "). Text (');
return true;
else {return false; }, "Ignore");
Supplemental IsEmpty function:
Null string judgment
function IsEmpty (V, allowblank) {return
v = = NULL | | | | (!allowblank? v = = "": false);
}
Handling of Validate Errorplacement:
Errorplacement:function (Error, Element) {
//Ignore the custom method error prompt
if (error.text () = = "Ignore") {return
;
}
}
Use in rules
Rules: {
Telephone: {
phone: []
},
Mobile: {
phone: []
}
}