Home  >  Article  >  Web Front-end  >  Detailed explanation of examples of jQuery Validate verifying multiple names

Detailed explanation of examples of jQuery Validate verifying multiple names

Y2J
Y2JOriginal
2018-05-14 16:41:012054browse

This article mainly introduces the method of jQuery Validate to verify multiple identical names. Friends who need it can refer to it

Introduction:

There is the following code in the form page

 <form>
  <input name="zhai"/><!-- 三个相同name的input -->
  <input name="zhai"/>
  <input name="zhai"/>
 </form>

jquery validate only verifies the first input box when validating multiple same names.

Solution 1:

Add the following code to the js corresponding to the form page. Only the current page can solve the verification of multiple names

 if ($.validator) {
   $.validator.prototype.elements = function () {
    var validator = this,
     rulesCache = {};
    return $(this.currentForm)
    .find("input, select, textarea")
    .not(":submit, :reset, :image, [disabled]")
    .not(this.settings.ignore)
    .filter(function () {
     if (!this.name && validator.settings.debug && window.console) {
      console.error("%o has no name assigned", this);
     }
     rulesCache[this.name] = true;
     return true;
    });
   }
  }

Solution 2:

Modify the source file and all pages can verify multiple names

1: Modify the jquery.validate.js file

Use ctrl+F to find this.name in rulesCache CommentRemove the following code.

elements: function() {
   var validator = this,
    rulesCache = {};
   // select all valid inputs inside the form (no submit or reset buttons)
   return $(this.currentForm)
   .find("input, select, textarea")
   .not(":submit, :reset, :image, [disabled]")
   .not( this.settings.ignore )
   .filter(function() {
    if ( !this.name && validator.settings.debug && window.console ) {
     console.error( "%o has no name assigned", this);
    }
    // 注释掉这里
    // select only the first element for each name, and only those with rules specified
    //if ( this.name in rulesCache || !validator.objectLength($(this).rules()) ) {
    // return false;
    //} 
    rulesCache[this.name] = true;
    return true;
   });
  },

Method 2: Modify the jquery.validate.min.js file

Use ctrl+F to find (c[this.name]= !0,!0)})

 return !this.name && b.settings.debug && window.console && console.error("%o has no name assigned", this),
//this.name in c || !b.objectLength(a(this).rules()) ? !1 : (c[this.name] = !0, !0)//注释这行
c[this.name] = !0, !0 //添加这行

【Related recommendations】

1. Javascript Free Video Tutorial

2. Single Line JS Implementing mobile terminal money format verification

3. Instance tutorial of vue v-model form control binding

4. Bootstrap form validation formValidation Detailed explanation of examples

5. The bug and processing method of offsetWidth in JS

The above is the detailed content of Detailed explanation of examples of jQuery Validate verifying multiple names. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn