savinger
savinger

Reputation: 6714

Can the input tag's name attribute simply be an integer?

Are the names of input tags allowed to simply be integers?

<input type="text" name="34" />

Just asking in case I were a lazy programmer. Or if I have a ton of arbitrary fields and it's not important what they are named.

Upvotes: 3

Views: 3772

Answers (4)

NamiW
NamiW

Reputation: 1692

Yes and no. Using integer is valid, yet it will be converted into a string.

The content attribute is the attribute as you set it from the content (the HTML code) and you can set it or get it via element.setAttribute() or element.getAttribute(). The content attribute is always a string even when the expected value should be an integer. For example, to set an element's maxlength to 42 using the content attribute, you have to call setAttribute("maxlength", "42") on that element.

ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes

Upvotes: 2

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201588

Yes, the name attribute is declared as taking CNAME value, which means any string of characters, without imposing constraints. HTML5 does not change this, except by disallowing the empty string; its definition explicitly says: “Any non-empty value for name is allowed”.

People sometimes confuse the name attribute with the id attribute, upon which there are various constraints depending on HTML version (e.g., some versions forbid a value that starts with a digit).

Upvotes: 6

bfavaretto
bfavaretto

Reputation: 71918

It's valid in HTML5, but I'd avoid it. However, your code is still not valid HTML5 because of the self-closing tag. It should be:

<input type="text" name="34">

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

Yes, they can. I wouldn't recommend it, but there's nothing wrong with using a number as a name attribute.

Upvotes: 2

Related Questions