473,322 Members | 1,703 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,322 software developers and data experts.

if(document.forms[f].element[e].type == "checkbox")

I want to know what's wrong with this code (I'm an amateur programmer).
I'm trying to check if every field has a value or if checkboxes/radios
have at least one item checked on each group (yes, you know, with the
same name in the HTML tag).
I want to have a generic code which I can fit to every document.

The problem is that it never got into the line:
if(elm.type == "checkbox" || elm.type == "radio")
I've put an alert(elm.type)


function checkallfields() {

var anychecked = true;

for(f = 0; f < document.forms.length ; f++) {
frm = document.forms[f];
for(e = 0; e < frm.elements.length; e++) {
elm = frm.elements[e];
if(elm.type == "checkbox" || elm.type == "radio") {
var passcheck = false;
for(c = 0; c < frm[elm.name].lenght; c++) {
n = e + c;
passcheck = passcheck || frm.element[n].checked;
}
e += c
anychecked = anychecked && passcheck;
} else {
if(elm.value == "") anychecked = anychecked && false;
}
}
}
return anychecked;
}

Well, here's the form which I'm doing the tests with:

<form method="POST" action="process.php"
onsubmit="return checkallfields()">

<input type="hidden" name="cat" value="1">
e-mail:<input type="text" name="email_respuesta" size="20">

<br>textarea:<textarea rows="2" name="textarea1" cols="20"></textarea>
<br>checkboxes:<input type="checkbox" name="checkboxes1" value="A">
<input type="checkbox" name="checkboxes1" value="B">
<br>radios:<input type="radio" value="V1" checked name="radios1">
<input type="radio" name="radios1" value="V2">
<br><input type="submit" value="Send">
<input type="reset" value="Clear">
<input type="button" value="check" onclick="alert(checkallfields());">
</form>

Thank you very much!
Jul 20 '05 #1
5 21168
In article <B_*********************@news.ono.com>, ph****@netscape.net
enlightened us with...
I want to know what's wrong with this code (I'm an amateur programmer).
I think it has a couple typos and doesn't check radio buttons right,
assuming you copied/pasted.
if(elm.type == "checkbox" || elm.type == "radio") {
var passcheck = false;
for(c = 0; c < frm[elm.name].lenght; c++)
length, not lenght.

{ n = e + c;
passcheck = passcheck || frm.element[n].checked;


frm.elements[n], not frm.element[n]

Can't check if it's checked this way for radio buttons.
Radio buttons are an array because they have the same name. Checkboxes
are not (unless more than one with same name).
Need array subscript for radio buttons. Loop through with another for
loop. Would be like
frm.elements[n][i].checked
for radios.

See archives and another post I made a few days ago on how to loop
through radio buttons.

-------------------------------------------------
~kaeli~
All I ask for is the chance to prove that money
cannot make me happy.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
Jul 20 '05 #2
Lee
PhiSYS said:

I want to know what's wrong with this code (I'm an amateur programmer).
I'm trying to check if every field has a value or if checkboxes/radios
have at least one item checked on each group (yes, you know, with the
same name in the HTML tag).
I want to have a generic code which I can fit to every document.

The problem is that it never got into the line:
if(elm.type == "checkbox" || elm.type == "radio")
I've put an alert(elm.type)
When I put an alert right after that "if", I see the alert.
What might be causing trouble is the typo of "length":
for(c = 0; c < frm[elm.name].lenght; c++) {


Jul 20 '05 #3
DU
kaeli wrote:
In article <B_*********************@news.ono.com>, ph****@netscape.net
enlightened us with...
I want to know what's wrong with this code (I'm an amateur programmer).

I think it has a couple typos and doesn't check radio buttons right,
assuming you copied/pasted.

if(elm.type == "checkbox" || elm.type == "radio") {
var passcheck = false;
for(c = 0; c < frm[elm.name].lenght; c++)

length, not lenght.


The code given still does not make sense. There is no length attribute
for checkbox. When elm is a checkbox, then frm[elm.name].length can not
be executed.
{
n = e + c;

n = e + c;
is a bad coding practice that is always bound to create confusion, more
time spent into debugging code. No meaningful, intuitive variable
identifiers.

DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/
- Resources, help and tips for Netscape 7.x users and Composer
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
http://www10.brinkster.com/doctorunc...e7Section.html

Jul 20 '05 #4
In article <bl**********@news.eusc.inter.net>, drunclear@hot-R-E-M-O-V-
E-mail.com enlightened us with...

The code given still does not make sense. There is no length attribute
for checkbox. When elm is a checkbox, then frm[elm.name].length can not
be executed.


Right. Normally.
Unless, however, he has several checkboxes of the same name.
Which he does.

See his original post, including html source.

-------------------------------------------------
~kaeli~
All I ask for is the chance to prove that money
cannot make me happy.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
Jul 20 '05 #5
God! My code was catastrophic.
Thank you all.

I make it work with the following code:

function checkallfields() {

var anychecked = true;

for(f = 0; f < document.forms.length ; f++) {
frm = document.forms[f];
for(e = 0; e < frm.elements.length; e++) {
elm = document.forms[f].elements[e];
if((elm.type == "checkbox") || (elm.type == "radio")) {
var passcheck = false;
var elm_name = elm.name;
var elm_length = frm[elm_name].length;
for(c = 0; c < elm_length; c++) {
n = e + c;
passcheck = passcheck || frm.elements[n].checked;
}
e = e + (elm_length - 1)
anychecked = anychecked && passcheck;
} else {
if(elm.value == "") anychecked = anychecked && false;
}
}
}
return anychecked;
}
Jul 20 '05 #6

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

Similar topics

2
by: John E | last post by:
How do I get whether a checkbox is ticked in a form on a form's submission (true or false)? How do I use the Request object in ASP?
4
by: Matt | last post by:
In ASP page, there is a "SELECT ALL" button, when user click it, it will select all checkboxes. I am not sure should I use client-side code to do that? the following is my approach but it didnt...
3
by: F. Da Costa | last post by:
Hi, Does the following indeed imply that this event is NOT called when an <input type="checkbox" is toggled? This would thus imply the usage of the onClick handler instead (assuming an action...
7
by: Don | last post by:
Via JavaScript within the client page containing the checkbox, I'm trying to capture the value of a checkbox to make a cookie out of it. But, all I get is the defined "ON" value, even when it is...
1
by: soup_or_power | last post by:
I'm passing the return from window.open as a function argument and getting the error "missing ] after element list" when tested with FireFox. Here is the relevant code. Many thanks for your help....
3
by: davidkarlsson74 | last post by:
Error: document.getElementById("folderMenu").cells has no properties File: http://www.volkswagen.se/tillbehor/js/foldermenu.js Rad: 49 The function activates different DIV:s, but doesn't seem to...
7
by: Jaime Stuardo | last post by:
Hi all.. I have a DataGrid with checkboxes. In the header I have a "check all" checkbox. I'm wondering if there is an easy way to check all checkboxes using that checkbox. I could do it using...
1
by: spolsky | last post by:
try the the following code with Opera 9.01 (Windows). when clicked slightly faster than normal clicking, the toggler checkbox and other checkboxes displays differently although event method works...
1
by: rando1000 | last post by:
So I have this form that captures Me.CheckboxName.Value as part of an SQL statement. For one checkbox, it evaluates as 0, which is great, because I'm filling a Yes/No field with it. But for another...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.