473,545 Members | 2,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic Radio Group

I have a page that has n number of radio groups (yes/No)

how can i prevent the form being submitted if more than one radio group is
not selected?

By default all radio groups are unchecked

The problem i am facing is that i do not know how many yes/no radio groups
will be generated
Many Thanks

Craig
Jul 23 '05 #1
6 3265
Craig Keightley wrote:
I have a page that has n number of radio groups (yes/No)

how can i prevent the form being submitted if more than one radio group is
not selected?

By default all radio groups are unchecked

The problem i am facing is that i do not know how many yes/no radio groups
will be generated


function areAllRadiosChe cked(form){
var f=form.length,c ounter=0,c=0;
while(f--){
if(form[f].type=="radio" && (form[f].name !=form[f+1].name)){c++;}
if(form[f].type=="radio" && form[f].checked){count er++;}
}
return c==counter;
}

Mick
Jul 23 '05 #2
In article <42************ **********@news-text.dial.pipex .com>, do**@spam.me
enlightened us with...

The problem i am facing is that i do not know how many yes/no radio groups
will be generated


Your validation function has to loop through all the form elements, then, and
check them against type==radio. This assumes all radio elements are part of
this check.
Note that document.forms is an array that has a property called elements that
is an array.
document.forms['formname'].elements[]
or
document.forms[index].elements[]
or
document.formna me.elements[] (shortcut syntax may not work in all browsers)
(you get the idea)

There are a couple other ways to do this that are not as cross-browser (i.e.
require newer DOM methods such as getElementsByTa gname, getElementsByNa me, or
getElementById) . If you only need to support newer browsers, you can do
something like name them all consistently (i.e. all named optiongroupname _x
where x is an integer), write the final value of x to a javascript variable,
and do a looped getElementsByNa me("optiongroup name"+x).

The first is simpler. If you have a LOT of form elements that are NOT part of
the option groups you're interested in, though, the second can save you
processing time by resulting in a smaller loop. Since JS runs on the client,
the time it takes to do things is highly dependent on the user's machine.
Sometimes it's worth it to try to optimize things that loop.

--
--
~kaeli~
Frisbeetarianis m (n.), The belief that, when you die, your
soul goes up on the roof and gets stuck there.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #3
Craig Keightley wrote:
I have a page that has n number of radio groups (yes/No)

how can i prevent the form being submitted if more than one radio group is not selected?

By default all radio groups are unchecked

The problem i am facing is that i do not know how many yes/no radio groups will be generated
Many Thanks

Craig From the HTML 4.0 spec:
At all times, exactly one of the radio buttons in a set is checked. If
none of the <INPUT> elements of a set of radio buttons specifies
`CHECKED', then the user agent must check the first radio button of the
set initially.
Since user agent behavior differs, authors should ensure that in each
set of radio buttons that one is initially "on".
From Jakob Nielsen:


Always offer a default selection for radio button lists. By definition,
radio buttons always have exactly one option selected, and you
therefore shouldn't display them without a default selection.
(Checkboxes, in contrast, often default to having none of the options
selected.)

Jul 23 '05 #4
RobB wrote:
Craig Keightley wrote:
I have a page that has n number of radio groups (yes/No)
[...]
From the HTML 4.0 spec:


At all times, exactly one of the radio buttons in a set is checked. If
none of the <INPUT> elements of a set of radio buttons specifies
`CHECKED', then the user agent must check the first radio button of the
set initially.
Since user agent behavior differs, authors should ensure that in each
set of radio buttons that one is initially "on".
From Jakob Nielsen:


Always offer a default selection for radio button lists. By definition,
radio buttons always have exactly one option selected, and you
therefore shouldn't display them without a default selection.
(Checkboxes, in contrast, often default to having none of the options
selected.)


To which could be added that if there is a simple yes/no choice, then
a checkbox is the answer. It requires only one control, not two, and
requires no JavaScript or additional code to ensure exactly two states.

Most user agents do not enforce the standard so it is common to
have yes/no radio buttons with neither selected, which suggests that
there are really three states - yes, no and 'no answer', which infers
three radio buttons where 'no answer' is checked by default.

--
Rob
Jul 23 '05 #5
Thanks for all the feedback and apologies for the late reply

Just to update:
The feedback form has to be radio buttons and do are not checked by default
(customer's spec not mine)

The system will only be viewed internally on a network using Internet
explorer

The radio buttons have subsequent javascript onClick events when selected
(innerHTML shows/hides part of the page)

I am going to try the code submitted by Mick and post an update:

function areAllRadiosChe cked(form){
var f=form.length,c ounter=0,c=0;
while(f--){
if(form[f].type=="radio" && (form[f].name !=form[f+1].name)){c++;}
if(form[f].type=="radio" && form[f].checked){count er++;}
}
return c==counter;
}
Craig
"RobG" <rg***@iinet.ne t.auau> wrote in message
news:42******** *************** @per-qv1-newsreader-01.iinet.net.au ...
RobB wrote:
Craig Keightley wrote:
I have a page that has n number of radio groups (yes/No)

[...]

From the HTML 4.0 spec:


At all times, exactly one of the radio buttons in a set is checked. If
none of the <INPUT> elements of a set of radio buttons specifies
`CHECKED', then the user agent must check the first radio button of the
set initially.
Since user agent behavior differs, authors should ensure that in each
set of radio buttons that one is initially "on".
From Jakob Nielsen:


Always offer a default selection for radio button lists. By definition,
radio buttons always have exactly one option selected, and you
therefore shouldn't display them without a default selection.
(Checkboxes, in contrast, often default to having none of the options
selected.)


To which could be added that if there is a simple yes/no choice, then
a checkbox is the answer. It requires only one control, not two, and
requires no JavaScript or additional code to ensure exactly two states.

Most user agents do not enforce the standard so it is common to
have yes/no radio buttons with neither selected, which suggests that
there are really three states - yes, no and 'no answer', which infers
three radio buttons where 'no answer' is checked by default.

--
Rob

Jul 23 '05 #6
Also forgot to mention that the radio groups are also dynamically named
eg:

<input name="auth3" type="radio" value="Y"
onClick="clearR eason('reason3' ,'reasonCell3') ">
No
<input name="auth3" type="radio" value="N"
onClick="showRe ason('reason3', 'reasonCell3')" >
<input name="auth4" type="radio" value="Y"
onClick="clearR eason('reason4' ,'reasonCell4') ">
No
<input name="auth4" type="radio" value="N"
onClick="showRe ason('reason4', 'reasonCell4')" >
<input name="auth5" type="radio" value="Y"
onClick="clearR eason('reason5' ,'reasonCell5') ">
No
<input name="auth5" type="radio" value="N"
onClick="showRe ason('reason5', 'reasonCell5')" >

.....etc
all i need to check that if all radio buttons are set to N, then set the
value of a hidden field (completeAuth) to N
Craig

"Craig Keightley" <do**@spam.me > wrote in message
news:42******** *************@n ews-text.dial.pipex .com...
Thanks for all the feedback and apologies for the late reply

Just to update:
The feedback form has to be radio buttons and do are not checked by
default (customer's spec not mine)

The system will only be viewed internally on a network using Internet
explorer

The radio buttons have subsequent javascript onClick events when selected
(innerHTML shows/hides part of the page)

I am going to try the code submitted by Mick and post an update:

function areAllRadiosChe cked(form){
var f=form.length,c ounter=0,c=0;
while(f--){
if(form[f].type=="radio" && (form[f].name !=form[f+1].name)){c++;}
if(form[f].type=="radio" && form[f].checked){count er++;}
}
return c==counter;
}
Craig
"RobG" <rg***@iinet.ne t.auau> wrote in message
news:42******** *************** @per-qv1-newsreader-01.iinet.net.au ...
RobB wrote:
Craig Keightley wrote:

I have a page that has n number of radio groups (yes/No)

[...]


From the HTML 4.0 spec:

At all times, exactly one of the radio buttons in a set is checked. If
none of the <INPUT> elements of a set of radio buttons specifies
`CHECKED', then the user agent must check the first radio button of the
set initially.
Since user agent behavior differs, authors should ensure that in each
set of radio buttons that one is initially "on".

From Jakob Nielsen:

Always offer a default selection for radio button lists. By definition,
radio buttons always have exactly one option selected, and you
therefore shouldn't display them without a default selection.
(Checkboxes, in contrast, often default to having none of the options
selected.)


To which could be added that if there is a simple yes/no choice, then
a checkbox is the answer. It requires only one control, not two, and
requires no JavaScript or additional code to ensure exactly two states.

Most user agents do not enforce the standard so it is common to
have yes/no radio buttons with neither selected, which suggests that
there are really three states - yes, no and 'no answer', which infers
three radio buttons where 'no answer' is checked by default.

--
Rob


Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3491
by: John Mullen | last post by:
I want to take the following HTLM and use javascript to turn on radio buttons if checkbox is checked, can I do this with javascript (maybe onClick or an array) or do i need a server side script ? <li>ABACAVIR SULFATE</li> <INPUT NAME="ingredient0" TYPE=checkbox VALUE="ABACAVIR SULFATE"><br> <input name="ABACAVIR SULFATE AMOUNT" type="radio"...
1
6892
by: Rick | last post by:
After being frustrated with this issue on several occasions I think I found the secret sauce for solving the issue after reading a few different messages about what others thought and trying a combination of them all. What worked for me was the following: 1. Put a Break Point in MFC source file DLGDATA.CPP on line 338 which is:...
22
7927
by: Saul | last post by:
I have a set of radio buttons that are created dynamically, after rendered I try loop thru this set by getting the length of the set, but I keep getting an error stating the element is undefined. I am using getElelementsByName since these are radio buttons, but it seems that the dynamic element is not seen!!! This is my code... please let...
7
3515
by: Jerim79 | last post by:
My situation is that I have a form that asks the user for a number. Next, I execute a while loop that displays a group of questions the amount of times the customer entered. For instance, the loop looks this: while ($Number!=0){ <input type="radio" name="Age" value="20-30">20-30 <input type="radio name="Age" value="30-40">30-40 <input...
2
5881
by: dpazza | last post by:
Hi, I'm creating a quiz on using a form in VB 2005 express. I have four sets of questions and answers (labels and radio buttons) and I change between which set of questions is currently shown on the form by changing the visible state of the radio buttons and labels utilising back and next buttons. E.g. Next button makes current radio...
3
18684
by: creative1 | last post by:
Here is how you create a complex data report that involves parent and child commands and you can update information at runtime. Its pretty straight forward to work with simple queries; however, working with complex reports is tricky Assumption: Reader of this article have basic knowledge of creating data reports. Creating a Parent-Child...
0
3371
bmallett
by: bmallett | last post by:
First off, i would like to thank everyone for any and all help with this. That being said, I am having a problem retrieving/posting my dynamic form data. I have a form that has multiple options within options. I have everything being dynamically named from the previously dynamically named element. (I hope this makes sense.) I am not able to...
0
1607
by: LavanyaM | last post by:
Hi, In our app we need to create dynamic radio buttons. There should be three radio buttons and this set is dynamic accroding to the data this is my code: <th width="5%" class="twolbcolors"><html:radio property="button" value="confirm"></</html:radio></th> <th width="5%" class="twolbcolors"><html:radio...
13
10700
by: tommymo | last post by:
Hi everyone I'm new to this site and the world of ASP.Net C# programming. I have been learning controls and integrating them with a SQL database. So far I have been able to move along and understand static controls. However I would like to move on to attempt to create some dynamic controls. So I set out to work with a radiobuttonlist...
0
7805
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...
1
7416
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
7752
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...
1
5325
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
4944
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
3449
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
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1878
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
701
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.