My validation script I have written takes the name attribute from an input tag and puts it in an alert if that input is required and not filled out. EX:
<input type="text" name="dateRequested" class="required" />
So if this wasnt filled out by the user, they would see an alert saying 'dateRequested' is a required field. Having no space looks ugly, so, simply, I would like to know if it is possible to have a space in the name attribute....ex: name="Date Requested". And if this is possible, is it bad practice?
From HTML5 3.2. 3.1 The id attribute: The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. The value must not contain any space characters.
The simplest way to add a space in HTML (besides hitting the spacebar) is with the non-breaking space entity, written as or  .
And, of course, any data-* attribute value can contain spaces that are meaningful (including leading and trailing spaces) so those spaces have to be reflected when accessing the element using that attribute value.
To add real spaces to your text, you can use the character entity. Tip: The non-breaking hyphen (‑) is used to define a hyphen character (‑) that does not break into a new line.
Yes it is possible. (The name attribute is defined as containing CDATA which is "pretty much whatever the heck you like"*)
No, it isn't bad practise. Well, not terrible practise. It might come back to bite you later though.
However … I would approach this by getting the id
of the input, finding the <label>
with a matching for
attribute, and using the text content of that. Labels are designed for human friendly labels, names are not. (Using jQuery (and not testing) for laziness: $('label[for="' + $(this).attr('id') + '"]').text()
)
Footnote: * counter intuitively there is a NAME token which is not what the name attribute takes, but it what the id attribute uses. There is also an ID token (which the id attribute doesn't take because it takes a NAME token).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With