I hope you had a "}" after the return 0?
I'm sure you're fully aware, having been told many times, that
you can't just use the field name or id to identify a field
in javascript.
One of the main reasons that people are continually told that they
shouldn't use a field's name or ID to identify a filed is that in many
cases you can, and so for some value of 'works' it does 'work'. The
problem with it is that there are some environments/contexts were it
won't work and so is neither reliable nor cross-browser.
You have to translate that information somewhere into something
the code understands.
In fact, this is a fundamental concept that you should
understand by now.
Irrespective of whether the logical flow of your code
works (and as I don't fully understand what you're trying
to check, I can't verify whether it will or not) I have no
idea what the one variable and three objects you're testing
here are.
No, the javascript code without the mark-up that creates the DOM with
which it interacts has relatively little meaning.
There are two ways to address objects relating to input
elements in a form from within javascript.
I can think of at least 6 obvious ways, and that means there will
probably be a dozen more.
One, which some people now consider outdated, is as a named
property of the parent form object,
and the other is using the element id.
That would be a vague distinction as a property of the FORM element
may exist with a name that corresponds with the ID of the element.
Presumably you mean accessing the form control using its ID with -
documentGetElementById -.
The W3C HTML DOM offers the - elements - property of the FORM element
as a collection of contained form control elements, that can be
integer indexed or referenced by name and/or ID. Being both DOM
standard and back-compatible with every scriptable web browser that
has ever known what a form is, this should be the preferred method of
accessing form controls.
If using the named property of form method, then you also
need to identify the form,
either as a member of the document's forms collection, or
as a named property of the document.
From a FORM element's - onsubmit - listener the form can be
anonymously referenced using the - this - keyword, and from the
intrinsic event listeners of form controls elements the - this.form -
will refer to the containing FROM element. Generally form validation
will involve one of these so there is normally no reason for the form
having a name/ID or there being any interest/reliance on the FORM's
index in the - document.forms - collection.
I have seen code that combines using the id of the form
and then referring to an input as a named property of that
form, which is to me illogical,
I don't see why. Strict HTML 4 does not allow FORM elements to have
NAME attributes, so there they cannot be referenced by name, and from
controls need to have NAME attributes if they are to be
'successful' (their values submitted to the server) so without some
other need (such as cross-referencing LABEL elements) adding ID
attributes to them could be superfluous.
as in my opinion if you're going to use element id
attributes you might as well use id at the input element
level.
An ID should be unique to the document, but a document could contain
multiple forms, and different forms may contain fields that
(logically) should have the same name.
Once you have identified the object that relates to an
input element on a form, there is as one and only one
common way to get the content of that element.
At least if you disregard radio buttons and checkboxes (neither of
which have "content" and their - value - properties are rarely of
primary interest while validating).
Richard.