473,546 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

if(document.for ms[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.le ngth; 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="retur n checkallfields( )">

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

<br>textarea:<t extarea rows="2" name="textarea1 " cols="20"></textarea>
<br>checkboxes: <input type="checkbox" name="checkboxe s1" value="A">
<input type="checkbox" name="checkboxe s1" value="B">
<br>radios:<inp ut 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 21182
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**********@n ews.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.le ngth; 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
29328
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
8401
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 work. <script language="JavaScript"> function selectAllCheckBox() { //alert(document.addzone.c1.value); document.addzone.c1.value = "on"; }...
3
37847
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 needs to be invoked on check/ uncheck). W3C Recomendation: 18 Scripts onselect = script The onselect event occurs when a user selects some text...
7
2935
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 unchecked. I'm using the following construct: document.cookie = "cpceRememberLoginCookie="+document.getPage1.rememberLogin.value+"; expires=Thr,...
1
18491
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. function blurCkbox(num, disp, but_id, sugg) { if (num == 1) { eval("self.opener.document.forms." + but_id + ".value=\'" + sugg + "\'");
3
9249
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 work on FireFox or Netscape. What could be wrong? The function: function setActiveTab(tabNo) {
7
2891
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 JavaScript code, but the main problem I have is that checkboxes ids aren't kept when the datagrid is rendered, for example, if the checkboxes have...
1
4013
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 fine to update the checkboxes. there is not any problem with IE 6 or FireFox 1.5. also, i used the double click event method to see if its the...
1
12556
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 checkbox, it evaluates as NULL, which is not so good. I know I can program around this with an If statement (or a number of other ways), but...
0
7435
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...
1
7461
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
6030
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...
1
5361
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...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3492
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...
0
3472
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1922
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
0
747
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.