473,915 Members | 5,000 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:

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!
Jul 20 '05 #1
5 4058
"Weston C" <west8on[at]cann8central.Re moveEights.org> wrote in message
news:3e******** *************@n ews.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[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....


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.Re moveEights.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:

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!


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.selectedInd ex].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 getElementByNam e/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*******@hotm ail.com> wrote in message
news:60******** *************** ***@posting.goo gle.com...
<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.Re moveEights.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:

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!

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
12625
by: Stan Brown | last post by:
http://oakroadsystems.com/nonce/zonk.htm (12 lines) > http://validator.w3.org/check?charset=%28detect+automatically%29&doctype=Inline&uri=http%3A%2F%2Foakroadsystems.com%2Fnonce%2Fzonk.htm&outline=x The W3C validator says "INPUT is not allowed here". I'm sure my mistake is really obvious, but I just can't see it. I'd appreciate another pair of eyes pointing out the problem. For convenience, here's the entire file. I've stripped it...
12
3261
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 warning about the generic font family I've got "a" sort of sheet that is "valid" but that pesky warning to use a generic font-family is still there. I can't move the body font-family declaration any higher up the the sheet without sending it the...
15
2140
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> <body> <script language="javascript"> <!-- var temp = new Array(2) var status = new Array();
2
8913
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 if(args.value == "0"){args.IsValid = false;) however it seemed like it never even fired that validator, even though the submit button CausesValidation = true,
38
3514
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 similar to innerHTML property eg. (my guess) setInnerNodeAsText or sth... ? I want to write function which will be dynamically updateing some of my select boxes. My question is: (2.1) Can i use innerHTML property of SELECT (or even incorporate
11
3139
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 non-trivial exercise. inserting '&lt;' and '&gt;' causes the browser to write the text to the page as literal text rather than as the intended script element. Using escape codes seemed to work (makes it standard compliant) but the text is not written to...
17
1538
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 any ADODB or ADOX stuff. What I'm trying to do is retrieve all the properties of all the components of a database. The GetSchema method goes a long way but (so far) I'm missing how to get the default value of a column. In the table that's returned...
1
1443
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
1377
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? I brought this up a while back, but with this new thing I just saw, thought I'd ask again. See sample below from EnterpriseLibrary
0
10039
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9881
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11354
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10542
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9732
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5943
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3368
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.