"DB McGee" <no*****@noreply.com> wrote in message
news:fL*******************@news01.bloor.is.net.cab le.rogers.com...
<snip>
1) Move the form validation to the <form>'s onsubmit handler -
you don't need to pass a reference to the form:
<form action="thanks.php" method="post"
onsubmit="return contact_validate()">
On the contrary, calling the validation function from the onsubmit
handler is an ideal opportunity to anonymously pass a generalised
validation function a reference to the FORM element that it is to be
applied to. In the internally generated event handling function that is
created using the onsubmit attribute string the - this - keyword is
always a reference to the FORM element to which the function is assigned
as a method:-
onsubmit="return contact_validate(this);"
- Removes the need to be interested in the name of the form.
<input type="text" name="name" id="name" size="25"/>
<snip>
"name" could be a bad name for a form element and also for the ID of an
element as the W3C HTML DOM specification requires that named and/or
IDed properties are made available as named properties of the FORM
element, so the above INPUT element would be available as a property of
the form under the name "name". However, the FORM element is also
required to have a property with the name "name" that represents the
name of the form.
Implementations may choose to replace the name property of the form with
a reference to the INPUT element with the name "name" or they might not,
the grounds for that decision are not specified. Generally it is unwise
to give form elements names (or IDs) that correspond with existing
properties of a FORM element, especially if those properties are going
to be used. As property names are case sensitive it is only necessary,
for exempt, to capitalise the name/ID to avoid the risk of conflicts.
2) Update your validation function
- reference the 'name' field using document.getElementById()
<snip>
If the point of the function is to provide a general function for use
with multiple forms, and the ID of the element is to be hard coded in
the function then the strategy will not work as the getElementById
method is required to return the first element on the page with the
corresponding ID, so it will always return a reference to the INPUT
element IDed as "name" in the first form on the page. But HTML requires
that an ID be unique on the page so hard coding an ID string into a
function is not viable if the function is intended to act with more than
one element.
The result is that if the string used to look up the reference to an
INPUT element is to be hard coded into a general validation function
then an ID cannot be used, and so getElementById is an inappropriate
method of acquiring the required reference.
Given a need to resolve a reference to a named INPUT element within a
form, where the same method and name string must be used with several
forms, the best place to look-up the reference to the input element is
as a named property of the - elements - collection of the form. An
approach that conforms with the W3C HTML DOM specification and is also
back-compatible with every JavaScript capable browser that also
understands what a form is:-
function contact_validate(formRef) {
var field_errors = "The following field(s) are required: \n\n";
var errors = 0;
if ( formRef.elements[ 'name' ].value == '') {
field_errors = field_errors+"Name - Please enter your name \n";
errors++;
}
if ( errors > 0 ) {
alert( field_errors);
return false;
}
return true
}
Richard.