473,545 Members | 2,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamic form validation / multiple forms

Good evening all,

I'm a relatively new to javascript, but I've been working with
ColdFusion and PHP for years so I'm not necessarily ignorant, just
stuck and frustrated. Using ColdFusion I'm using an include to pull in
form elements (text fields, checkboxes, etc...) multiple times on a
single page.

The included page does not have a form tag of it's own, but the root
page has uniquely named forms for validation. Imagine it like this:

<form name="form1">
<cfinclude tempalte="formt emplate">
</form>

<form name="form2">
<cfinclude tempalte="formt emplate">
</form>

I've got a simple check-one-box-and-check-em-all script INSIDE the
included template. I want to reference very specifically the form name,
and only check the boxes inside that form, not the others.
Unfortunately, I'm not syntax savvy and my attempts to use:

<form name="form1">
<input type="checkbox" id="chkAllNorth "
onClick="chkAll ('chkAllNorth', 'northChk',this .form)">
</form>

does nothing. I can remove the this.form from the function entirely,
but it checks ALL the specified boxes with that ID in ALL the forms on
the page. Any help is appreciated. Here is the script:

function chkAll(thisBox, group,thisForm) {
var inputs = document.thisFo rm.getElementsB yTagName("input ");

if(document.thi sForm.getElemen tById(thisBox). checked == true){
for (i=0;i<inputs.l ength;i++){
if(inputs[i].id == group){
inputs[i].checked = 1;
}
}
}else{
for (i=0;i<inputs.l ength;i++){
if(inputs[i].id == group){
inputs[i].checked = 0;
}
}
}
return true;
}

Aug 10 '06 #1
4 7248
je*******@gmail .com wrote:
Good evening all,

I'm a relatively new to javascript, but I've been working with
ColdFusion and PHP for years so I'm not necessarily ignorant, just
stuck and frustrated. Using ColdFusion I'm using an include to pull in
form elements (text fields, checkboxes, etc...) multiple times on a
single page.

The included page does not have a form tag of it's own, but the root
page has uniquely named forms for validation. Imagine it like this:

<form name="form1">
<cfinclude tempalte="formt emplate">
</form>

<form name="form2">
<cfinclude tempalte="formt emplate">
</form>

I've got a simple check-one-box-and-check-em-all script INSIDE the
included template. I want to reference very specifically the form
name, and only check the boxes inside that form, not the others.
Unfortunately, I'm not syntax savvy and my attempts to use:

<form name="form1">
<input type="checkbox" id="chkAllNorth "
onClick="chkAll ('chkAllNorth', 'northChk',this .form)">
</form>
Is this a representation of ColdFusion code or mark-up that gets sent to
the browser, and if the latter where are the INPUT element's NAME and
VALUE attributes? Without a NAME and a VALUE the value of the checkbox
will not be sent back to the server, so how can you know it value for
server-side (re)validation?
does nothing.
Nothing at all, or have you just failed to observe the (any) error
messages produced by the browser?
I can remove the this.form from the function entirely,
That sounds like precisely the last thing you want to do as -
this.form - is (in the context of intrinsic event attribute code) the
one reliable method of getting a reference to the form that contains the
INPUT element (so excluding the other form).
but it checks ALL the specified boxes with that ID in ALL
the forms on the page.
Not the code here.
Any help is appreciated. Here is the script:

function chkAll(thisBox, group,thisForm) {
If the even handler above is fired the - thisForm - parameter will be a
reference to the FORM element.
var inputs = document.thisFo rm.getElementsB yTagName("input ");
^^^^^^^^
In this context the - thisForm - Identifier refers to a property of the
document with the name 'thisForm', and is completely unrelated to the -
thisForm - parameter (look up "javascript bracket notation" on google).
The symptoms you describe could only be attributed to there being an
object on the generated page that has an ID or NAME of 'thisForm', such
that the browser is creating a reference to that element as a named
property of the document, that is a container of both of the FORM
elements on the page. Otherwise this code should error-out at this line
an none of the checkboxes should be checked.

<snip>
inputs[i].checked = 1;
<snip>
inputs[i].checked = 0;
<snip>

The checked property of INPUT elements is of boolean type so setting the
values to 1 or 0 will just necessitate type-conversion. You may as well
set them to true or false directly.

Generally, when debugging client-side javascript it is a good idea to
look at (and post in preference to server side code) the mark-up and
code actually sent to the browser, as that is where the errors are most
easily identified. And the javascript console or error dialogs provided
by the browser would usually be expected to show pertinent error
messages (and diagnosis requires knowledge of all actually error
messages, or a positive confirmation that there are none).

Richard.
Aug 11 '06 #2
This is raw code, as it exists in my cfm, not rendered by firefox/ie.

Originally, I wrote the validation script for a single form....named
"form", so there wasn't an additional variable in the function. so
"document.thisF orm" was simply "document.f orm" and it worked perfectly
with no java/browser errors whatsoever. As I originally stated "does
nothing" I should've said that an error is generated: "thisForm has no
properties". This is likely due to the bracket notation you mentioned.
Bracket notation typically escapes me, as I said, I'm relatively new to
JS, so thanks for that.

The input elements do indeed have names and ID's so please don't
misunderstand. In the interest of not providing gobs upon gobs of code,
I chose to explain that the include statements had properly syntaxed
form input elements, primarily checkboxes.

Should I then take to your meaning that the function should read
thusly?:

function chkAll(thisBox, group,thisForm) {
var inputs = document[thisForm].getElementsByT agName("input") ;

if(document[thisForm].getElementById (thisBox).check ed == true){
for (i=0;i<inputs.l ength;i++){
if(inputs[i].id == group){
inputs[i].checked = 1;
}
}
}else{

for (i=0;i<inputs.l ength;i++){
if(inputs[i].id == group){
inputs[i].checked = 0;
}
}
}
return true;
And the checkbox trigger should be?:
<form name="form1">
<input type="checkbox" id="chkAllNorth "
onClick="chkAll ('chkAllNorth', 'northChk',this .form)">
<all the other boxes here id="northChk">
</form>

Will that succesfully pass the form name (in this case form1) to the
function and check all the boxes with the id 'northChk' in only the
form 'form1'? I apologize that I cannot try it for myself at the moment
because I am out of the office and do not have the pages available to
me until the morning.

Aug 11 '06 #3
je*******@gmail .com said the following on 8/10/2006 9:03 PM:
This is raw code, as it exists in my cfm, not rendered by firefox/ie.

Originally, I wrote the validation script for a single form....named
"form", so there wasn't an additional variable in the function. so
"document.thisF orm" was simply "document.f orm" and it worked perfectly
with no java/browser errors whatsoever. As I originally stated "does
nothing" I should've said that an error is generated: "thisForm has no
properties". This is likely due to the bracket notation you mentioned.
Bracket notation typically escapes me, as I said, I'm relatively new to
JS, so thanks for that.

The input elements do indeed have names and ID's so please don't
misunderstand. In the interest of not providing gobs upon gobs of code,
I chose to explain that the include statements had properly syntaxed
form input elements, primarily checkboxes.

Should I then take to your meaning that the function should read
thusly?:

function chkAll(thisBox, group,thisForm) {
var inputs = document[thisForm].getElementsByT agName("input") ;

if(document[thisForm].getElementById (thisBox).check ed == true){
for (i=0;i<inputs.l ength;i++){
if(inputs[i].id == group){
inputs[i].checked = 1;
}
}
}else{

for (i=0;i<inputs.l ength;i++){
if(inputs[i].id == group){
inputs[i].checked = 0;
}
}
}
return true;
And the checkbox trigger should be?:
<form name="form1">
<input type="checkbox" id="chkAllNorth "
onClick="chkAll ('chkAllNorth', 'northChk',this .form)">
<all the other boxes here id="northChk">
</form>

Will that succesfully pass the form name (in this case form1) to the
function and check all the boxes with the id 'northChk' in only the
form 'form1'? I apologize that I cannot try it for myself at the moment
because I am out of the office and do not have the pages available to
me until the morning.
ID's must be unique in the page so any behavior you get with multiple
ID's is guess work.

function chkAllNorth(for mName,elementNa meToCheck){
//Now, simply loop through formName checking element
//type and name, if it is elementNameToCh eck, then
//you set it to checked. Since you don't have time
//to be bothered to check code, I won't waste time
//writing the code for you.
}

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 11 '06 #4
je*******@gmail .com wrote:
This is raw code, as it exists in my cfm, not rendered by
firefox/ie.
Which means that it is not the code that is producing the errors. The
code that the browser actually gets is the only code that is interesting
for diagnosing the problems that produce errors on the browser.
Originally, I wrote the validation script for a single
form....named "form",
The name "form" is not particularly good, partly because form controls
already have a property named 'form', and partly because it says nothing
about what the form is for.
so there wasn't an additional variable
in the function. so "document.thisF orm" was simply
"document.f orm" and it worked perfectly with no java/browser
errors whatsoever. As I originally stated "does nothing"
I should've said that an error is generated: "thisForm has no
properties".
That is not what the error actually said. it said - document.thisFo rm
has no properties (or something similar). The _precise_ error message
may not mean much to you yet but you will eventually see that they have
a very close relationship with what is going wrong on the browser.
This is likely due to the bracket notation you mentioned.
Not due to, but avoidable with.

<snip>
The input elements do indeed have names and ID's so
please don't misunderstand.
I understood what I read. If that was a misunderstandin g then I was
reading the wrong material (and only you have the power to influence
that).
In the interest of not providing gobs upon gobs of
code, I chose to explain that the include statements
had properly syntaxed form input elements, primarily
checkboxes.
So you were saying 'I have some code I would like help debugging, and
here is some other code that may be similar in some respects'.
Should I then take to your meaning that the function
should read thusly?:

function chkAll(thisBox, group,thisForm) {
var inputs = document[thisForm].getElementsByT agName("input") ;
Not if you pass - this.form - as the third argument. The - form -
property of form controls is a reference to the form element, and any
property of the document being referenced by the form's name attribute
is also a reference to the form element, so the - document[ someName ] -
part is redundant as soon as you are passing the reference to the form
into the function from the event handler. If you passed -
this.form.name - into the fucntion then what you would get is the name
of the form element (if it has one) but then using that name to look-up
the reference is a bit silly as the reference was where the name was
look up prior to passing it.
if(document[thisForm].getElementById (thisBox).check ed == true)
Comparing the boolean - checked - property with boolean true to give a
boolean result for the - if - expression is redundant as the comparison
will always produce the dame result as the - checked - property already
has.

Even if the - document[thisForm] - was being resolved into a reference
to FORM element, FORM elements do not have a - getElementById - method
(only documents do), and the document level method will not work
properly unless the element ID is unique.
{ for (i=0;i<inputs.l ength;i++){
The variable - i - is not declared in this function, that makes it
global. It does not need to be global and so should be subject to the
axiom; no variable should be given any more scope than it absolutely
needs. It should be a function local variable (as that is the most local
scope available), so declared within the function. Habitually using
global variables a loop counters is particularly problematic as it
becomes easy for a function to be called in the body of one loop that
itself uses a global loop counter with the same name. Unexpected
consequences include infinite loops and loops that end sooner than
expected.
if(inputs[i].id == group){
inputs[i].checked = 1;
<snip>

You decided to ignore my advice to assign true/false instead of 1/0?
Will that succesfully pass the form name (in this case form1)
No.
to the function and check all the boxes with the id 'northChk'
in only the form 'form1'?
IDs should be unique within an HTML document. You should fix that design
misconception before you do anything else.

Given the mark-up code you (appear to) have, passing the reference to
the form as:- onClick="chkAll ('chkAllNorth', 'northChk',this .form);" -
the fucntion:-

function chkAll(thisBox, group, theForm){
var i, inputs = theForm.getElem entsByTagName(" INPUT");
var checkIt = thisForm.elemen ts[thisBox].checked;
for (i=0;i<inputs.l ength;i++){
if(inputs[i].id == group){
inputs[i].checked = checkIt;
}
}
return true;
}

- should do the job of the original.
I apologize that I cannot try it for myself at the
moment because I am out of the office and do not have the pages
available to me until the morning.
Richard.

Aug 11 '06 #5

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

Similar topics

1
2316
by: Florian Peer | last post by:
Hello! I am trying to set up an form, which at the end makes a vacation package. It has a certain amount of pages, depending on what the client wants. I would like to read some background articles, how you make such forms, since I thought of different ways to approach it, but I don't know which is the best one..
12
2440
by: CJ | last post by:
Why won't this work? I am passing the name of the form (I have two that use this validation script) but I keep getting an error. Error reads: "document.which_form.name is null or not an object" HTML----------- Form is ----> <form action="thanks.php" method="post" name="contact_form" id="contact_form"> Name -------> <input type="text"...
1
1544
by: Toby Miller | last post by:
I have this form validation that I'm trying to build, but it's not working properly. A dynamic function to the onsubmit event for a form. the result of that function (true/false) should then be returned to pass or fail the submission of that form. What is happening is that in Firefox and IE returning false doesn't stop the form submission....
3
2932
by: CAD Fiend | last post by:
Hello, Well, after an initial review of my database by my client, they have completely changed their minds about how they want their form. As a result, I'm having to re-think the whole process. My Current Form (6 tabs): - Owner, Property, Title, Docs, Queries, & Reports - User is able to see (while navigating through the tabs) above in...
8
2004
by: George Meng | last post by:
I got a tough question: The backgroud for this question is: I want to design an application works like a engine. After release, we can still customize a form by adding a button, and source code for the button. (This is done by the form itself, not by using VS.Net) (button and source code should be a record in database, these information...
10
4944
by: Mark Winter | last post by:
Hello, I am wondering if someone could point me in the right direction to figure out how to create a dynamic form. I have been developing programs in perl and oracle and have recently switched to php. I need to create a form that has four of five seperate pick list on it. Each item in each of the pick list depend on the users previous...
5
2538
by: matt | last post by:
hello, i am on an interesting project. in this project, i have to create dynamic data-entry forms for offline-users to fill out, save locally, and eventually postback to our app (when back online). data validation is required on the form. i had looked at using PDF-forms for this.. Adobe's "LifeCyle Forms" would work perfectly. with it...
11
2967
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether certain fields match certain criteria, and inform the user in different ways when the data is wrong (offcourse, this will be checked on posting the...
10
5695
by: gweasel | last post by:
What is the best way to apply a Validation Rule - or rather, where is the best place to put it? Is there an advantage to putting it on the field in the table vs setting the validation rule on the form the control is on? Basically I have a number of controls in a form that are required, and to check it I am setting the Validation Rule to...
0
7496
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...
0
7428
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...
0
7685
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. ...
1
7452
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...
0
6014
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...
0
3467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1039
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
738
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...

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.