Reputation: 6714
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
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
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
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
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