473,408 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Radio Button validation - NOT USUSAL

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
13 1544
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
But I need to have those marked "no" (3 remaining questions) to capture
no as the answer....

Jul 23 '05 #3
;) 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
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
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
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
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
What a brilliant man! Thanks. Works like a charm.

Jul 23 '05 #10
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: David Cameron | last post by:
I noticed that using an HTMLInputRadioButton and specifying a value to be an empty string (""), this is overridden by ASP.Net which set the value of the control to be the same as the ID of the...
6
by: Blinky | last post by:
Hi all, I have a dynamically generated page that can have 1 or more radio buttons. I am using javascript with onsubmit in the form statement to make sure a radio button is selected before...
9
by: wreed | last post by:
I have a for loop seen below.... var the_form = document.getElementById(formName); for(var i=0; i<the_form.length; i++) { var temp = the_form.elements.type; if (temp == "radio") { for (x =...
1
by: Jerim79 | last post by:
I have a simple 3 page registration form. One form, one "data validation" script and one "insert into database" script. The customer bounces back and forth from the form to the verification script...
5
by: swatidesai0407 | last post by:
hi im validating radio buttons i create dis radio button in php based on some how many records of my query. i wrote a javascript to validate this buttons. wat i do is dat wen no radio button...
0
by: Gary W. Smith | last post by:
Hello, I have a simple form with a radio button group on it and I need to do some basic validation based upon which button is checked. Here is a little more detail. if button one is...
8
by: photoboy | last post by:
I have racked by brain long enough on this, so now I need the help of someone who knows what they are doing. Here is what I am trying to achieve: First, I have two radio buttons (both unchecked)...
4
by: pureadrenaline | last post by:
Hey Guys, Please could anyone help me out with the following form I need to create a validation on the email field only if the user checked the radio button named Email. Thanks in advance. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...

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.