Connecting Tech Pros Worldwide Forums | Help | Site Map

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

PhiSYS
Guest
 
Posts: n/a
#1: Jul 20 '05
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!

kaeli
Guest
 
Posts: n/a
#2: Jul 20 '05

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


In article <B_jeb.50448$FN3.3503392@news.ono.com>, phisys@netscape.net
enlightened us with...[color=blue]
> I want to know what's wrong with this code (I'm an amateur programmer).[/color]

I think it has a couple typos and doesn't check radio buttons right,
assuming you copied/pasted.
[color=blue]
> if(elm.type == "checkbox" || elm.type == "radio") {
> var passcheck = false;
> for(c = 0; c < frm[elm.name].lenght; c++)[/color]

length, not lenght.

{[color=blue]
> n = e + c;
> passcheck = passcheck || frm.element[n].checked;[/color]

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
-------------------------------------------------
Lee
Guest
 
Posts: n/a
#3: Jul 20 '05

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


PhiSYS said:[color=blue]
>
>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)[/color]

When I put an alert right after that "if", I see the alert.
What might be causing trouble is the typo of "length":
[color=blue]
> for(c = 0; c < frm[elm.name].lenght; c++) {[/color]

DU
Guest
 
Posts: n/a
#4: Jul 20 '05

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


kaeli wrote:
[color=blue]
> In article <B_jeb.50448$FN3.3503392@news.ono.com>, phisys@netscape.net
> enlightened us with...
>[color=green]
>>I want to know what's wrong with this code (I'm an amateur programmer).[/color]
>
>
> I think it has a couple typos and doesn't check radio buttons right,
> assuming you copied/pasted.
>
>[color=green]
>> if(elm.type == "checkbox" || elm.type == "radio") {
>> var passcheck = false;
>> for(c = 0; c < frm[elm.name].lenght; c++)[/color]
>
>
> length, not lenght.
>[/color]

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.
[color=blue]
> {
>[color=green]
>> n = e + c;[/color][/color]


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

kaeli
Guest
 
Posts: n/a
#5: Jul 20 '05

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


In article <blcn0h$e7c$1@news.eusc.inter.net>, drunclear@hot-R-E-M-O-V-
E-mail.com enlightened us with...[color=blue]
>
> 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.
>[/color]

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
-------------------------------------------------
PhiSYS
Guest
 
Posts: n/a
#6: Jul 20 '05

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


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;
}
Closed Thread