473,394 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Trying to Write a Generic Form Validator

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:

FieldsToValidateByForm = {};
FieldsToValidateByForm['contact'] = ["FirstName",
"LastName","State","Email"];

function validate(form)
{
problemFields = new Array();
returnval = true;
FieldsToValidate = FieldsToValidateByForm[form.id];

for(i=0; i < FieldsToValidate.length; i++) {
fieldInQuestion = form[FieldsToValidate[i]];
if(fieldInQuestion.value.length < 1) //problem spot?
problemFields.push(FieldsToValidate[i]);
}

if(problemFields.length > 0) {
returnval = false;
warn(problemFields); /* tells user they're missing a field,
that's all */
}

return returnval;
}
What I think is happening (not sure) is that the expression
form[fieldsToValidate[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 misunderstanding 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.org
(remove eights to email me)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #1
5 4010
"Weston C" <west8on[at]cann8central.RemoveEights.org> wrote in message
news:3e*********************@news.frii.net...
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. What I think is happening (not sure) is that the expression
form[fieldsToValidate[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 misunderstanding of how the DOM
works here. Unfortunately, I can't seem to find a good enough reference
to set me straight....


Try this:
http://www.w3.org/TR/2000/WD-DOM-Lev...language-bindi
ng.html (I have this saved locally because it's so useful!)

In particular the "Object HTMLDocument" section, and its "forms" property
which is an HTMLCollection (which is also fully documented there)

You should be able to follow the DOM through and figure it out from there...

Nige
Jul 20 '05 #2
Weston C <west8on[at]cann8central.RemoveEights.org> wrote in message news:<3e*********************@news.frii.net>...
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:

FieldsToValidateByForm = {};
FieldsToValidateByForm['contact'] = ["FirstName",
"LastName","State","Email"];

function validate(form)
{
problemFields = new Array();
returnval = true;
FieldsToValidate = FieldsToValidateByForm[form.id];

for(i=0; i < FieldsToValidate.length; i++) {
fieldInQuestion = form[FieldsToValidate[i]];
if(fieldInQuestion.value.length < 1) //problem spot?
problemFields.push(FieldsToValidate[i]);
}

if(problemFields.length > 0) {
returnval = false;
warn(problemFields); /* tells user they're missing a field,
that's all */
}

return returnval;
}
What I think is happening (not sure) is that the expression
form[fieldsToValidate[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 misunderstanding 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.org
(remove eights to email me)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Many ways.

This is available easily from the form object. DOM is just a
distraction.

I strobe through the form elements, inspecting type for how to handle
the object value. For example, a "select-one" <select> object has a
..selectedIndex property, so obj[obj.selectedIndex].value would index a
value. .checked for others....
Something I noticed of ie long ago is that it can lose the form object
reference when being handed another function deeper. So I always
reconstruct the reference within the next function by the sloppy: var
oForm = eval( document[oForm.name] ), which requires the form at least
be named, or use the getElementByName/ID.
One could have the onBlur/onChange inspect the form object element
upon use of that element by the user. If empty or an off checkbox,
then set a global JS flag, maybe bSubmitForm = false.
Jul 20 '05 #3
"John" <jh*******@hotmail.com> wrote in message
news:60**************************@posting.google.c om...
<snip>
Something I noticed of ie long ago is that it can lose the
form object reference when being handed another function deeper.
So I always reconstruct the reference within the next function
by the sloppy: var oForm = eval( document[oForm.name] ), which
requires the form at least be named,....

<snip>

Whatever you think might be justifying this operation you are almost
certainly utterly mistaken. In order for this operation to return a
reference to a named form and assign it to the - oForm - variable -
oForm - must start off holding a reference to that form, else the name
will not resolve. That makes the whole operation pointless to start
with. The additional use of the - eval - function demonstrates a
complete lack of understanding of the actions of that function and is
completely futile.

It would be better to understand the actions of the - eval - function
before recommending its use to anyone but, as the use of - eval - is
almost never necessary and its appearance in JavaScript source code is
usually indicative of an ill-conceived approach, it would be better to
never recommend the use of - eval - at all.

Richard.

--

Example JavaScript DOM listings for: Opera 7.11,
Mozilla 1.2 and ICEbrowser 5.4
<URL: http://www.litotes.demon.co.uk/dom_root.html >
Jul 20 '05 #4
Checkout the form validation script located at:
http://www.javascript-coder.com/html...lidation.phtml
It is a general form validation script.
You can use the script for most of the validations.

Hope this helps..

Prasanth,
http://www.javascript-coder.com
Javascript Coder is a tool that can generate
code for a number of cool JavaScript Features.

Weston C <west8on[at]cann8central.RemoveEights.org> wrote in message news:<3e*********************@news.frii.net>...
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:

FieldsToValidateByForm = {};
FieldsToValidateByForm['contact'] = ["FirstName",
"LastName","State","Email"];

function validate(form)
{
problemFields = new Array();
returnval = true;
FieldsToValidate = FieldsToValidateByForm[form.id];

for(i=0; i < FieldsToValidate.length; i++) {
fieldInQuestion = form[FieldsToValidate[i]];
if(fieldInQuestion.value.length < 1) //problem spot?
problemFields.push(FieldsToValidate[i]);
}

if(problemFields.length > 0) {
returnval = false;
warn(problemFields); /* tells user they're missing a field,
that's all */
}

return returnval;
}
What I think is happening (not sure) is that the expression
form[fieldsToValidate[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 misunderstanding 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.org
(remove eights to email me)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jul 20 '05 #5
The chapter on form validation from Danny Goodmans "Javascript and DHTML
Cookbook" are online at:

Part 1: http://www.webreference.com/programm...dhtml/chap8/1/

Part 2:
http://www.webreference.com/programm...8/2/index.html
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Stan Brown | last post by:
http://oakroadsystems.com/nonce/zonk.htm (12 lines) >...
12
by: Abby Hart | last post by:
It's got me beat. I'm convinced that I've seen css validation reports without the warning during my trawls thru the net trying to teach meself this stuff ... but I can't get a report without the...
15
by: tabonni | last post by:
I want to check each button groups and save the checked value into a 2 dimensional array. But, it doesn't work. Could anyone tell me what's wrong with my code. My code is as follow: <html>...
2
by: Wes Weems | last post by:
I'd like to have a 3 values in a drop down, and have a validator not allow a form submit unless its 2 of the 3 values (eg, one of the values says "select one") I used custom validator and did a...
38
by: Luke Matuszewski | last post by:
Welcome I have read the in the faq from jibbering about the generic DynWrite, but i also realized that is uses only innerHTML feature of HTML objects. (1) Is there a DOM function which is very...
11
by: Michael Powe | last post by:
How can I make an XHTML-compliant form of an expression in this format: document.write("<scr"+"ipt type='text/javascript' src='path/to/file.js'>"+"</scr"+"ipt>"); this turns out to be a...
17
by: Sam Malone | last post by:
I am trying to get details from a database. I really want to use only native VS.NET managed code "stuff" (just cuz I want to) and avoid any interop stuff. So, I'm trying to do this without using...
1
by: colleen1980 | last post by:
Hi: I am trying to pull all the values from the listbox. But the ASP code shows only the last record. Needs help HTML <html> <head>
0
by: sloan | last post by:
Is anyone else using the Microsoft Practices EnterpriseLibrary conventions for naming FILES (.cs) as far as a non-generic and generic implementation is concerned? Anyone have an alternative? ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.