Click to See Complete Forum and Search --> : Issue with validating radio buttons


mikeysweet
09-15-2003, 05:19 PM
I am trying to do the following with radio buttons:


On screen the user sees a question and picks from 4 radio buttons.
There are 13 of these questions each with 4 radio buttons.

Now here are the parts that have me stumped.

Part 1
If the user does not answer all of them, then it is ok and can be submitted.
If the user has selected an answer for all of the questions then it is ok
If the user only answered 1 or more, but not all of them, then prompt the user to answer them all.

Part 2
Is there a way to click on a link/button/anything to clear all the choices of all of the radio buttons but NOT clear off the whole form

These 2 have been driving me NUTS the whold weekend!!!!!!

Any help would be so greatly appreciated!!
Thanks
-Mike
mikeysweet@hotmail.com

Jona
09-15-2003, 05:28 PM
Originally posted by mikeysweet
Part 2
Is there a way to click on a link/button/anything to clear all the choices of all of the radio buttons but NOT clear off the whole form


<form action="" name="myForm"><div>
<input type="radio" name="radio1" value="a"> A.<br>
<input type="radio" name="radio2" value="b"> B.<br>
<input type="radio" name="radio3" value="c"> C.<br>
<input type="text" name="answer" value="What's your answer?"><br>
<input type="submit" value=" Submit "> | <input type="button" value=" Reset Radio Buttons " onClick="for(i=0; i<this.form.elements.length; i++){ if(this.form.elements[i].type=='radio' && this.form.elements[i].checked==true){ this.form.elements[i].checked=false; } }">
</div></form>


[J]ona

Jona
09-15-2003, 05:55 PM
Originally posed by mikeysweet
Part 1
If the user does not answer all of them, then it is ok and can be submitted.
If the user has selected an answer for all of the questions then it is ok
If the user only answered 1 or more, but not all of them, then prompt the user to answer them all.


<script type="text/javascript"><!--
function v(f){
var num=0;
for(j=0; j<f.elements.length; j++){
if(f.elements[j].type=="radio" && f.elements[j].checked==true){num+=1;}
if(f.elements[j].type=="radio" && f.elements[j].checked==false){num-=1;}
}
if(num==3){ alert("All boxes have been checked"); return true;}
if(num>=-1 && num<=3){ alert("Some boxes have been checked, but not all"); return false; }
if(num<=0){ alert("No boxes have been checked"); return true;}
}
//--></script></head>
<body>
<form name="myForm" onsubmit="v(this); return false;"><div>
<input type="radio" name="radio1" value="a"> A.<br>
<input type="radio" name="radio2" value="b"> B.<br>
<input type="radio" name="radio3" value="c"> C.<br>
<input type="text" name="answer" value="What's your answer?"><br>
<input type="submit" value=" Submit "> | <input type="button" value=" Reset Radio Buttons " onClick="for(i=0; i<this.form.elements.length; i++){ if(this.form.elements[i].type=='radio' && this.form.elements[i].checked==true){ this.form.elements[i].checked=false; } }">
</div></form>

mikeysweet
09-16-2003, 10:13 AM
Jona-
Thanks for your help in this.
The Reset Radio Buttons works beautifully!!

Part 2 though doesn't seem to work for my application.
I took the code you put on here and edited it in the fashion of my form that I am working on.

I changed it to have 3 sets of 3 choices and no matter how i fill them out, I always get the "No Boxes Have been checked"

It should submit if no questions are answered
It should submit if all questions are answered
It should not submit if some questions are answered

I hope that you can help me out in this... I think i lost a good portion of my hair trying to figure this out!!
Thanks

<script type="text/javascript"><!--
function v(f){
var num=0;
for(j=0; j<f.elements.length; j++){
if(f.elements[j].type=="radio" && f.elements[j].checked==true){num+=1;}
if(f.elements[j].type=="radio" && f.elements[j].checked==false){num-=1;}
}
if(num==3){ alert("All boxes have been checked"); return true;}
if(num>=-1 && num<=3){ alert("Some boxes have been checked, but not all"); return false; }
if(num<=0){ alert("No boxes have been checked"); return true;}
}
//--></script></head>
<body>
<form name="myForm" onsubmit="v(this); return false;"><div>
Question 1<br>
<input type="radio" name="radio1" value="a"> A.<br>
<input type="radio" name="radio1" value="b"> B.<br>
<input type="radio" name="radio1" value="c"> C.<br>
Question 2<br>
<input type="radio" name="radio2" value="a"> A.<br>
<input type="radio" name="radio2" value="b"> B.<br>
<input type="radio" name="radio2" value="c"> C.<br>
Question 3<br>
<input type="radio" name="radio3" value="a"> A.<br>
<input type="radio" name="radio3" value="b"> B.<br>
<input type="radio" name="radio3" value="c"> C.<br>
<input type="text" name="answer" value="What's your answer?"><br>
<input type="submit" value=" Submit "> | <input type="button" value=" Reset Radio Buttons " onClick="for(i=0; i<this.form.elements.length; i++){ if(this.form.elements[i].type=='radio' && this.form.elements[i].checked==true){ this.form.elements[i].checked=false; } }">
</div></form>

Jona
09-16-2003, 11:55 AM
<script type="text/javascript"><!--
function v(f){
var num=0;
for(j=0; j<f.elements.length; j++){
if(f.elements[j].type=="radio" && f.elements[j].checked==true){num+=1;}
if(f.elements[j].type=="radio" && f.elements[j].checked==false){num-=1;}
}
alert(num);
if(num==-3){ alert("All boxes have been checked"); return true;}
if(num<=-1 && num>-9){ alert("Some boxes have been checked, but not all"); return false; }
if(num<=-9){ alert("No boxes have been checked"); return true;}
}
//--></script></head>
<body>
<form name="myForm" onsubmit="v(this);"><div>
Question 1<br>
<input type="radio" name="radio1" value="a"> A.<br>
<input type="radio" name="radio1" value="b"> B.<br>
<input type="radio" name="radio1" value="c"> C.<br>
Question 2<br>
<input type="radio" name="radio2" value="a"> A.<br>
<input type="radio" name="radio2" value="b"> B.<br>
<input type="radio" name="radio2" value="c"> C.<br>
Question 3<br>
<input type="radio" name="radio3" value="a"> A.<br>
<input type="radio" name="radio3" value="b"> B.<br>
<input type="radio" name="radio3" value="c"> C.<br>
<input type="text" name="answer" value="What's your answer?"><br>
<input type="submit" value=" Submit "> | <input type="button" value=" Reset Radio Buttons " onClick="for(i=0; i<this.form.elements.length; i++){ if(this.form.elements[i].type=='radio' && this.form.elements[i].checked==true){ this.form.elements[i].checked=false; } }">
</div></form>

mikeysweet
09-16-2003, 12:33 PM
This works BEAUTIFULLY!!!!!
Thanks so much for this.
You have saved my butt
-Mike

Jona
09-16-2003, 01:23 PM
Welcome thou art.

[J]ona