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?
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
idof the input, finding the<label>with a matchingforattribute, 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).