I'm trying to write a generic/reusable form validator in Javascript...
just something that checks to make sure required fields have a value. By
generic I mean I don't want to explicitly reference the name/id of the
form or the name of any of the data fields within a "validation "
function.
My first shot seems to have some errors in it:
FieldsToValidat eByForm = {};
FieldsToValidat eByForm['contact'] = ["FirstName" ,
"LastName","Sta te","Email"];
function validate(form)
{
problemFields = new Array();
returnval = true;
FieldsToValidat e = FieldsToValidat eByForm[form.id];
for(i=0; i < FieldsToValidat e.length; i++) {
fieldInQuestion = form[FieldsToValidat e[i]];
if(fieldInQuest ion.value.lengt h < 1) //problem spot?
problemFields.p ush(FieldsToVal idate[i]);
}
if(problemField s.length > 0) {
returnval = false;
warn(problemFie lds); /* tells user they're missing a field,
that's all */
}
return returnval;
}
What I think is happening (not sure) is that the expression
form[fieldsToValidat e[i]] is not giving me what I want: a reference to
the object corresponding to the form field with the same name. In
otherwords, I must have some fundamental misunderstandin g of how the DOM
works here. Unfortunately, I can't seem to find a good enough reference
to set me straight....
-W
~==~
http://weston.canncentral.org/
Taking Pictures During Dreams
weston8[at]cann8central.or g
(remove eights to email me)
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!