12

Is it valid markup and is it valid to the HTML/HTTP spec? If I wanted to have

<form>
<input name="email" />
<input name="email" />
<input name="email" />
</form>

is that valid?

1
  • 1
    By definition, radio buttons in the same group have the same name.
    – MrWhite
    Commented Feb 28, 2011 at 9:58

3 Answers 3

10

As stated on SO:

The browsers are OK with it. However, how the application library parses it may vary.

Programs are supposed to group identically named items together. While the HTML specification doesn't explicitly say this, it is implicitly stated in the documentation on checkboxes:

"Several checkboxes in a form may share the same control name. Thus, for example, checkboxes allow users to select several values for the same property."

On a side note, in PHP you can have form fields with the same name if you use PHP's arry syntax for the field name:

<form>
<input name="email[]" />
<input name="email[]" />
<input name="email[]" />
</form>

This will cause the browser to send over the form fields in a syntax that PHP will use to populate an array for these fields: $_REQUEST['email']. If you use PHP this is the preferred way to handle it.

9

HTML5

The non-normative section 4.10.1.3 Configuring a form to communicate with a server explicitly says that it is valid:

Multiple controls can have the same name; for example, here we give all the checkboxes the same name, and the server distinguishes which checkbox was checked by seeing which values are submitted with that name — like the radio buttons, they are also given unique values with the value attribute.

The normative version of this is simply that it is not forbidden anywhere, and the form submission algorithm says exactly what request should be generated:

1

Yes the markup

<form>
<input name="email">
<input name="email">
<input name="email">
</form>

is valid: in fact it is possible and it is OK to use the same name multiple times into a form or even into the same document, but it is not the use of the same ID that must be unique into the whole document/page. AFAIK, an exception is in HTML5 where is required that a form's name has to be unique if the page contains multiple forms. But the elements' names even doesn't matter if in the same form or in different forms in the same document can be duplicated. The address https://html.spec.whatwg.org/multipage/forms.html#attr-form-name reports the spec and you can find the following info:

The name attribute represents the form's name within the forms collection. The value must not be the empty string, and the value must be unique amongst the form elements in the forms collection that it is in, if any.

The form collection can be accessed/selected with document.forms.

Notice that the html elements will be rendered in any case by browsers, even if they have duplicate ids, because browser are error tolerant in this case, but the problem is the formal validation and if the elements have to be manipulated with some script: in fact notice for example that:

  • in case of multiple elements sharing the same id, only the first hit will be returned by selection functions/methods like getElementById() or querySelector()etc.
  • in case of multiple elements sharing the same name all elements will be returned by selection functions/methods like getElementsByName() (notice the "s" at the end of Elements) or querySelectorAll(). Of course querySelector() will only return the first hit in any case.
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.