Using the Jquery.validate customization method to achieve "mobile phone number or fixed at least one" logic verification _jquery

Source: Internet
Author: User

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: []
        }
      }

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.