473,795 Members | 2,512 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 21196
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
8438
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
9277
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
2901
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
12611
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
10214
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
10164
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
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7540
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
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
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.