On Tue, 30 Nov 2004 06:45:25 GMT, RobG <rg***@iinet.net.auau> wrote:
Robert Smith wrote:
I have a very basic form validation script, which wont work due to
XHTML Strict not allowing me to use the name attribute on a form. Here
is part of my code: [...]
The following works in both Firefox and IE. I tried changing the
id attribute of the input to a name attribute and it still worked
fine. So I guess this is one of those "can't replicate the
problem" problems.
I think you misunderstand. The name attribute is perfectly acceptable on a
form *control*, and if you intend to submit the data that control
contains, it *must* be specified just as must with HTML. The name
attribute on the form element is the problem as XHTML Strict doesn't allow
it.
In fact, name attributes are only allowed on images, forms and links for
backwards compatibility with old browsers like NN4 which were written
before the introduction of the id attribute. Unless you care about them,
the name attribute should *only* be used for form controls.
[snip]
<form action="" id="feedback">
[snip]
if(document.getElementById('feedback')) {
You don't need to use getElementById. The forms collection can be
subscripted with numbers, names, or ids. With a string subscript[1], the
user agent will attempt to find a matching id first. Only if that fails
will a name look-up be performed.
document.forms['feedback'].elements['first_name']
[snip]
Mike
[1] Every expression inside square brackets is treated as a string,
including numeric literals and object references. However, if the string
can be converted to a number, then back to a string, and still match the
original value exactly, it is considered to be an array index, not a
property. So:
'10' -> 10 -> '10' '10' array index
'05' -> 5 -> '5' '05' property name
'fg' -> NaN -> 'NaN' 'fg' property name
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.