...

View Full Version : Function



Shabina
06-14-2002, 05:58 PM
:confused:

<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.

:rolleyes:

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.

JohnKrutsch
06-14-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(document.f.elements[i].type=="radio"){
if(temp!=document.f.elements[i].name || i==document.f.elements.length-2){
if(ok==0 && temp!="") bad +=" "+temp+"\n";
temp=document.f.elements[i].name;
ok=0;
}
ok+=document.f.elements[i].checked;
}
}

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

}
//-->
</script>

<form name="f" onsubmit="return radioCheck()">
gender:
<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">
</form>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum