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

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

小云云
Release: 2018-01-12 09:07:58
Original
1927 people have browsed it

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>
Copy after login

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;
    });
   }
  }
Copy after login

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;
   });
  },
Copy after login

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 //添加这行
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template