In comp.lang.javascript message said:
phpCodeHead said the following on 10/11/2007 6:53 PM:
The document.forms collection is a collection of *all* form objects in
a document. Whether explicitly declared or browser forced. You can loop
through it and get the forms name or id - if it has one - and then go
through validation. A better construct would be a case/switch if there
are more than two forms in the document.
Unless the website has only one page containing forms, the
document.forms collection is, of course, not a collection of all forms
in a site.
I am curious what the purpose of validating more than one form though
as only one gets submitted at the time.
That is not what was asked for. The request was for a single script
that would validate all of the forms, but did not call for all to be
validated at once. It's not certain why he wants to have a single
script.
However, if over several pages there are many forms of comparatively few
distinct varieties[*], it may well be appropriate to put all of the
validation code into an include file, invoking (as he suggests) an
appropriate routine for each.
So the include file (see <URL:
http://www.merlyn.demon.co.uk/js-
nclds.htm>) can contain a General Form Validator Function along the
lines of
var STFVFO = {ID0:Fn0, ID1:Fn1, ,,,, IDN:FnN} // an Object
function GFVF(FormID, parameter_array)
{ STFVFO[FormID](document.getElementById(FormID), parameter_array) } ;
function Fn0(F, PA) { // validate a form of type #0
... }
....
Now in many cases much of the validation of a form is of a few simple
types : date, number-in-range, alphanumeric, etc.; and so a
parameterised function can use one set of code for all such types, and
can be provided with auxiliary functions for special considerations
(such as "if in Ramadan, date must not be Tuesday").
Code of that nature is demonstrated in <URL:
http://www.merlyn.demon.co.u
k/js-valid.htm#VFF>.
[*] The GUM (pronounced as /goom/[+]) web-site could[#] have many pages
each with a form for adding items described on that page into a list of
purchases, for example.
[+] But is goom pronounced like boom, doom, loom; or like good or goof
or gook or googly or goop ?
[#] AFAICS, it doesn't. But it does chime at (or near) the quarter-
hour. And Santa flies past occasionally.
It's a good idea to read the newsgroup c.l.j and its FAQ. See below.