473,804 Members | 3,273 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 21200
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
29337
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
8440
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"; } </script>
3
37878
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 in a text field. This attribute may be used with the INPUT
7
2944
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, 01-Jan-2015 00:00:00 GMT"; The actual checkbox value passed in the <form> POST reflects the actual...
1
18514
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
9280
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
2904
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 the name chkNumid, when the datagrid is rendered, the checkboxes become dgDataGrid_ctl02_chkNumId,
1
4023
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 source but that does not help even. Opera 9.01 seems to be slow at updating checkboxes visually. am i...
1
12616
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 shouldn't Me.CheckboxName.Value be consistent across like controls?
0
9706
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9584
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10583
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10337
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
6854
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
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

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.