By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,625 Members | 1,720 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,625 IT Pros & Developers. It's quick & easy.

Radio Button validation - NOT USUSAL

P: n/a
Scenario:

There are four questions, each with radio buttons to mark the choice
"yes" or "no". I need only one yes per FORM (so if 2 is marked yes,
there is no need for any others to be, etc) so validation must look for
a single yes, then alert if there is more than one....problem is, I
can't seem to get it to work.

Jul 23 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
wrote on 08 mrt 2005 in comp.lang.javascript:
Scenario:

There are four questions, each with radio buttons to mark the choice
"yes" or "no". I need only one yes per FORM (so if 2 is marked yes,
there is no need for any others to be, etc) so validation must look for
a single yes, then alert if there is more than one....problem is, I
can't seem to get it to work.


If you give them the same name, only one will be yes.

<form>
<input type='radio' name='myRadio' value='1' checked> one<br>
<input type='radio' name='myRadio' value='2'> two<br>
<input type='radio' name='myRadio' value='3'> three<br>
<input type='radio' name='myRadio' value='4'> four<br>
</form>

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #2

P: n/a
But I need to have those marked "no" (3 remaining questions) to capture
no as the answer....

Jul 23 '05 #3

P: n/a
;) jepp, weird. There are some ways to check and find out but i've
never worked
that really out because 'radios' look pretty shitty and 'buttons'
are much easier
to handle if you write a little wrapper that only one of Your
buttons bar can be
checked. (like hold the names in an array and onClick use a function
to switch
them to 'off-state', f.ex. change value or color and enable the
number originally
was clicked on.)
=> onclick="myfunc(buttname)"
==> function myfunc(name){
for(var ii=0;ii<nr_of_buttons;ii++){
if(name==name[ii]){ highlight }
else{turnoff }
}
}

same works with radios too, but as i think they dont look sexy
anyhow...

Jul 23 '05 #4

P: n/a
another workaround ;)

hows about
<input type="radio" name="like" value="YES" onClick="Tlike='YES'">Yes
<input type=hidden value="no" name="Tlike">
....and just look for the T{names} ??? ;)

Jul 23 '05 #6

P: n/a
but how would you validate that there is only one yes??
You would still have to do &&, right?

Sorry....I haven't done validation in years and something like this is
the one that is stopping me.

Jul 23 '05 #7

P: n/a
ty******@doas.ga.gov wrote:
There are four questions, each with radio buttons to mark the choice
"yes" or "no". I need only one yes per FORM (so if 2 is marked yes,
there is no need for any others to be, etc) so validation must look
for a single yes, then alert if there is more than one....problem is,
I can't seem to get it to work.


Try this:

<SCRIPT
SRC="http://www.mattkruse.com/javascript/validations/source/validations.js">
</SCRIPT>
<SCRIPT>
function validate() {
var theForm = document.forms["sectionV"];
var formFields = new Array("test","like","work","alike");
var yesCount = 0;
for (i in formFields) {
if (getInputValue(theForm.elements[formFields[i]])=="YES") {
yesCount++;
}
if (yesCount > 1) {
alert("Too many yes answers!");
return false;
}
}
return true;
}
</SCRIPT>

This uses my validations.js file which makes getting input values easier and
more generic. You can copy it to your own server for real use.

Hope that helps.

--
Matt Kruse
http://www.JavascriptToolbox.com
Jul 23 '05 #8

P: n/a
Peach wrote:
but how would you validate that there is only one yes??
You would still have to do &&, right?

Sorry....I haven't done validation in years and something like this is the one that is stopping me.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>untitled</title>
<style type="text/css">

</style>
<script type="text/javascript">

function oneyes()
{
var f = document.forms[0],
els = f.elements,
el,
i = 0,
grp = [];
while (el = els[i++])
if (/radio/.test(el.type)
&& /yes/i.test(el.value))
{
grp.push(el);
el.onclick = function()
{
var el, i = 0;
while (el = grp[i++])
if (el.checked && el != this)
{
alert('Please, only one "yes".');
els[this.name][1].checked = true;
return false;
}
}
}
}

onload = oneyes;

</script>
</head>
<body>
<form>
<ol>
<li><input type="radio" name="Q1" value="yes" />yes<br />
<input type="radio" name="Q1" value="no" checked="checked" />no</li>
<br /><br />
<li><input type="radio" name="Q2" value="yes" />yes<br />
<input type="radio" name="Q2" value="no" checked="checked" />no</li>
<br /><br />
<li><input type="radio" name="Q3" value="yes" />yes<br />
<input type="radio" name="Q3" value="no" checked="checked" />no</li>
<br /><br />
<li><input type="radio" name="Q4" value="yes" />yes<br />
<input type="radio" name="Q4" value="no" checked="checked" />no</li>
<br /><br />
</ol>
</form>
</body>
</html>

Jul 23 '05 #9

P: n/a
What a brilliant man! Thanks. Works like a charm.

Jul 23 '05 #10

P: n/a
Peach wrote:
What a brilliant man! Thanks. Works like a charm.


Here's another one, based on RobB's example, that modifies the
checked radio buttons so only one 'yes' is ever checked and all
the rest are 'no'.

It may suit ... or not.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>one yes</title>
<script type="text/javascript">

function initForm(f){
var els = document.forms[f].elements;
var i = els.length;
while (i--) {
if (/radio/.test(els[i].type)
&& /yes/i.test(els[i].value)) {

els[i].onclick = function() {
var j = els.length
var el = els[j];
while (j--) {
var el = els[j];
if ( /radio/.test(el.type)
&& /yes/i.test(el.value)
&& el.checked
&& el != this) {
// Put up alert if wanted...
toggleYes(el);
}
}
}; // end of function
}
}
}

// Set current radio to off,
// next 'no' radio to on
function toggleYes(el){
el.checked = false;
while ( el.nextSibling // Prevent endless loop
&& ( !/radio/.test(el.type)
|| !/no/i.test(el.value))) {
el = el.nextSibling;
}
el.checked = true;
}
</script>
</head>
<body onload="initForm('q');">
<form name="q" action="">
<ol>
<li><input type="radio" name="Q1" value="yes">yes&nbsp;
<input type="radio" name="Q1" value="no"
checked="checked">no</li><br>
<li><input type="radio" name="Q2" value="yes">yes&nbsp;
<input type="radio" name="Q2" value="no"
checked="checked">no</li><br>
<li><input type="radio" name="Q3" value="yes">yes&nbsp;
<input type="radio" name="Q3" value="no"
checked="checked">no</li><br>
<li><input type="radio" name="Q4" value="yes">yes&nbsp;
<input type="radio" name="Q4" value="no"
checked="checked">no</li><br>
</ol>
<input type="reset">
</form>
</body>
</html>
--
Rob
Jul 23 '05 #11

P: n/a
Thanks to everyone for their help!

I used Matt Kruse's suggestion and it seemed to be the easiest way and
worked like a charm.

Jul 23 '05 #12

P: n/a
JRS: In article <11**********************@o13g2000cwo.googlegroups .com>
, dated Tue, 8 Mar 2005 06:13:18, seen in news:comp.lang.javascript,
ty******@doas.ga.gov posted :

There are four questions, each with radio buttons to mark the choice
"yes" or "no". I need only one yes per FORM (so if 2 is marked yes,
there is no need for any others to be, etc) so validation must look for
a single yes, then alert if there is more than one....problem is, I
can't seem to get it to work.


Rationally, that should be done with one set of four or five buttons,
the fifth being either "None of those" or hidden. One button may or may
not be appropriate for a default. Validation is then either unnecessary
or trivial. But I see that you have a .gov address.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #13

P: n/a
Wow....what a nice man you are Dr John. If you notice, each of the
quesiton serves a completely different question and, at my customers
request (no matter how trivial), I DO NOT want a "None of the Above"
and want the questions separated. If you could see the actual form, you
would better understand, but since you are not a "privileged" person, I
cannot share such information with you....ya know...government secrets
and all....

NOW....was there honestly ANY sense in being rude Dr John?

To everyone else who answered or attempted to answer the question I
asked instead of providing their rude commentary, thank you. Again,
Matt Kruse's repsonse is exactly what I was looking for. Thank you!

Jul 23 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.