3

I have an HTML form that I'm processing with Perl CGI, like so:

<form action="process.cgi" id="main">
  <input type="text" name="foo" class="required" />
  <input type="text" name="foo" class="required" />
  <input type="text" name="foo" class="required" />
</form>

And I'm validating it with the jQuery Validation plugin. Trouble is, when I call $('#main').valid(), it only checks that the first field is non-empty. How to I get it to check all the fields?

5
  • The name attribute is a way to uniquely identify a input element for javascript. You basically override it three times in a row with your current code, hence it won't work. w3schools.com/tags/att_input_name.asp Commented May 20, 2013 at 17:13
  • 2
    I don't believe that's correct. While it's true that the 'id' attribute must be unique, it is common practice to give several elements the same name in order to present them as an array to the handling system (in this case, Perl). See stackoverflow.com/questions/5518458/… Commented May 20, 2013 at 18:38
  • this is valid html, but jquery validate can't handle multiple elements with same name - some discussion here stackoverflow.com/q/931687/678338. If your html can't change then you need to change jquery validate Commented May 20, 2013 at 19:04
  • That makes sense, wasn't aware of this feature. Then it seems the jquery plugin doesn't consider this though and just selects the first matching and ignores the rest. Commented May 20, 2013 at 19:05
  • As @SeanMill stated: Internally, the jQuery Validate plugin requires a unique name attribute on all form inputs. Even if the name attribute is not used for anything, it still must be present and unique for this plugin to function properly. Commented May 20, 2013 at 21:09

1 Answer 1

2

OFFICIAL DOCUMENTATION:
jqueryvalidation.org/reference/#link-markup-recommendations

"Mandated: A 'name' attribute is required for all input elements needing validation, and the plugin will not work without this. A 'name' attribute must also be unique to the form, as this is how the plugin keeps track of all input elements. However, each group of radio or checkbox elements will share the same 'name' since the value of this grouping represents a single piece of the form data."


<input type="text" name="foo" class="required" />
<input type="text" name="foo" class="required" />
<input type="text" name="foo" class="required" />

Quote OP:

Trouble is, when I call $('#main').valid(), it only checks that the first field is non-empty. How to I get it to check all the fields?

It has nothing specifically to do with .valid() and it will fail the same using other jQuery Validate methods. That's because all three fields have the same name of foo. For this plugin each input must have a unique name.

DEMO: jsfiddle.net/xaFZj/


EDIT:

You cannot have duplicate names while using this plugin. This demo clearly shows that the plugin will fail when the name attribute is duplicated. There is no workaround since the name attribute is how the plugin keeps track of the form elements.

jsfiddle.net/ed3vxgmy/

The only exception is a radio or checkbox group where the elements in each grouping will share the same name.

Sign up to request clarification or add additional context in comments.

6 Comments

Is that documented somewhere?
@KitPeters, See the second paragraph under the MARKUP RECOMMENDATIONS section: "The name attribute is required for input elements, the validation plugin doesn't work without it." Since the plugin requires a name attribute to keep track of everything, then logically it would need to be unique name.
That makes sense. Pity it won't work for my application; seems like quite a nice piece of SW.
Sorry but required doesn't mean unique and this is actually nonsense that it can't work without unique names. There are many applications out there using multiple inputs. For example a language select box. It's obviously a requirement to have the name languages[] on all inputs. Please do not stop people from research.
Sorry @OzanKurt, you are simply wrong and should at least do your own research before making misguided comments and down-voting. You cannot have duplicate names on text input fields while using this plugin: jsfiddle.net/vqnawogr
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.