View Full Version : Function

Jun 14th, 2002, 05:58 PM

<form> tag contains <input type= radio> in a loop (ie) if a page contain many questions with radio buttons as options, and if any question left unchecked it should alert me a dialog box saying not clicked. If there is only one question in a page by wrinting a javascript code, it can be checked.


What javascript code should be implemented as soon as the submit button of the page is clicked, checking all the radio buttons be clicked and if one radio button is unchecked pop a dialog box.

Jun 14th, 2002, 07:39 PM
I am sure there are lots of ways to accomplish this but something along these lines ought to do it:

<script type="text/javascript">
function radioCheck(){
var temp=bad="", ok=0;
for(var i=0; i<document.f.elements.length; i++){
if(temp!=document.f.elements[i].name || i==document.f.elements.length-2){
if(ok==0 && temp!="") bad +=" "+temp+"\n";

if (bad!=""){
alert("Be sure to choose one of the following:\n\n"+bad);
return false;
return true;


<form name="f" onsubmit="return radioCheck()">
<input type="radio" name="gender" value="male"> male
<input type="radio" name="gender" value="female"> female
<br />
I like pizza:
<input type="radio" name="pizza" value="true"> true
<input type="radio" name="pizza" value="false"> false
<br />
I like chocolate:
<input type="radio" name="chocolate" value="true"> true
<input type="radio" name="chocolate" value="false"> false
<br />
<input type="submit" value="submit">