One validation script - multiple forms to validate

P

phpCodeHead

I know that this must be quite trivial to most of you, but my skills
in JavaScript are weak.

I am looking for a way to validate all forms throughout a website
through one validation script.

How do I determine the form's name (or id) value to implement
conditional validation?

i.e. if (form1) { validate like this }; if (form2) { validate like
this };

Thanks for any input you may have.....

Gene
 
G

GArlington

I know that this must be quite trivial to most of you, but my skills
in JavaScript are weak.

I am looking for a way to validate all forms throughout a website
through one validation script.

How do I determine the form's name (or id) value to implement
conditional validation?

i.e. if (form1) { validate like this }; if (form2) { validate like
this };

Thanks for any input you may have.....

Gene

Closer to the point of "generic" validation routine you should define
which validation methods are to be used on each field maybe based on
field name or any other attributes.
 
D

Dr J R Stockton

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top