Use the jquery. validate custom method to implement logic verification of "Enter at least one mobile phone number or fixed phone number", jqueryvalidate

Source: Internet
Author: User

Use the jquery. validate custom method to implement logic verification of "Enter at least one 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 : []        }      }




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.