Home  >  Article  >  Web Front-end  >  jQuery Validate verifies the sharing of multiple instances of the same name

jQuery Validate verifies the sharing of multiple instances of the same name

小云云
小云云Original
2018-01-12 09:07:581858browse

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

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 identical names.

Solution one:

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 two:

Modify the source file and all pages can verify multiple names

Method 1: Modify the jquery.validate.js file

Use ctrl+F to find this.name in rulesCache and comment it out 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 search (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:

Spring shiro bootstrap jquery.validate login and registration function implementation method

jQuery Validate form verification plug-in detailed explanation

jquery.validate.js Detailed explanation of how to handle multiple identical names

The above is the detailed content of jQuery Validate verifies the sharing of multiple instances of the same name. 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