Recent project development encountered such a demand "mobile phone number or landline to fill at least one" as shown in:
The Jquery.validate.js validation component used by the project, which currently does not support this "or" logical validation, defines itself as a
JQuery.validator.addMethod ("Phone",function(value, Element) {varMobile = $ ("#mobile"). Val ();//Mobile phone number varTelephone = $ ("#telephone"). Val ();//Fixed Telephone varMobilerule =/^ (13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8| 9]|18[0-9]|170) \d{8}$/; varTelephonerule =/^\d{3,4}-?\d{7,9}$/; //It 's not filled . if(IsEmpty (MOBILE) &&IsEmpty (telephone)) { //Custom errorTips$ ("#receivingMobile_tip"). AddClass ("Errorhint"). Text ("Please fill in the fixed phone or mobile number")); return false; } varMobilepass =false; varTelephonepass =false; //phone filled, fixed phone not filled if(!isempty (MOBILE) &&IsEmpty (telephone)) { if(!mobilerule.test (MOBILE)) { //Custom Error Hints$ ("#receivingMobilePhone_tip"). Removeclass ("Successhint"). AddClass ("Errorhint"). Text ("Cell phone number is not formatted"); return false; } Else{Mobilepass=true; } } //phone not filled, fixed phone filled if(IsEmpty (MOBILE) &&!IsEmpty (telephone)) { if(!telephonerule.test (telephone)) { //Custom Error Hints$ ("#receivingTelephone_tip"). Removeclass ("Successhint"). AddClass ("Errorhint"). Text ("Fixed phone format is incorrect"); return false; } Else{Telephonepass=true; } } if(Mobilepass | |Telephonepass) { //Custom Success Tips$ ("#receivingTelephone_tip"). Removeclass ("Errorhint"). AddClass ("Successhint"). Text ("); return true; } Else { return false; } }, "Ignore");
Supplemental IsEmpty function:
// empty string judgment function IsEmpty (V, allowblank) { returnnullfalse);}
Handling the errorplacement of validate:
function (Error, Element) { // Ignore custom method error prompt if (error.text () = = "Ignore") { return ; }
}
Use in rules
Rules: { Telephone: { phone: [] }, Mobile: { phone: [] } }
Transferred from: http://www.cnblogs.com/xiaoyangjia/p/3945007.html
"Reprint" [Jquery.validate] custom method to achieve "mobile phone number or fixed phone" logic verification