472,101 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,101 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 21009
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by F. Da Costa | last post: by
7 posts views Thread by Don | last post: by
1 post views Thread by soup_or_power | last post: by
7 posts views Thread by Jaime Stuardo | last post: by
1 post views Thread by spolsky | last post: by

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.