How can I make radio buttons mandatory fields?

baxtalo
baxtalo used Ask the Experts™
on
Hi Experts,
I have these radio buttons on my page and my users tend to skip checking the No value. How could I make it mandatory? Please see the code below, what do I have to add?
Thank you.
<html>
<head>
<title>Radio Button</title>
<script language="JavaScript" type="text/JavaScript">
<!--
function uncheckRadio() {
 var choice = document.form1.Sleepy;
 for (i = 0; i < choice.length; i++) {
  if ( choice[i].checked = true )
   choice[i].checked = false;
 }
}

function uncheckRadio1(){
 var choice = document.form1.Thirsty;
 for (i = 0; i < choice.length; i++) {
  if ( choice[i].checked = true )
   choice[i].checked = false;
 }
}

function uncheckRadio2(){
 var choice = document.form1.Hungry;
 for (i = 0; i < choice.length; i++) {
  if ( choice[i].checked = true )
   choice[i].checked = false;
 }
}

//-->
</script>
</head>

<body>

<form method="post" action="MyPage.asp">
<table>
<tr><td align="right">
<b>Are you sleepy?&nbsp;&nbsp;</b>
</td><td>
<label>
<input type="radio" name="Sleepy" value="Yes" ondblclick="uncheckRadio();"><b>Yes</b></label>
</td><td>&nbsp;&nbsp;&nbsp;
<label><input type="radio" name="Sleepy" value="No" ondblclick="uncheckRadio();"><b>No</b></label>
</td></tr>
<tr><td colspan="3">&nbsp;</td></tr>
<tr><td align="right">
<b>Are you thirsty?&nbsp;&nbsp;</b>
</td><td>
<label>
<input type="radio" name="Thirsty" value="Yes" ondblclick="uncheckRadio();"><b>Yes</b></label>
</td><td>&nbsp;&nbsp;&nbsp;
<label><input type="radio" name="Thirsty" value="No" ondblclick="uncheckRadio();"><b>No</b></label>
</td></tr>
<tr><td colspan="3">&nbsp;</td></tr>
<tr><td align="right">
<b>Are you hungry?&nbsp;&nbsp;</b>
</td><td>
<label>
<input type="radio" name="Hungry" value="Yes" ondblclick="uncheckRadio();"><b>Yes</b></label>
</td><td>&nbsp;&nbsp;&nbsp;
<label><input type="radio" name="Hungry" value="No" ondblclick="uncheckRadio();"><b>No</b></label>
</td></tr>
</table>
<input type="submit" name="Submit" id="flashingtext" value="Submit" />

</body>
</html>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Multitechnician
Top Expert 2014
Commented:

Author

Commented:
Great! Thank you very much. Now I just have to figure out how to add more 'onsubmit' commands to the form, because I have more things on the page, not only radio buttons. I'll submit a new question. Thanks again.
leakim971Multitechnician
Top Expert 2014

Commented:
You're welcome! Thanks for the points!

You can do :

onsubmit="return (validation1() && validation2());"

Or :

onsubmit="return validationS();"
with :

function validations() {
   var v1 = validation1();
   var v2 = validation2();
   return v1 && v2;
}

Open in new window

Commented:
<input submit ... onsubmit="isRadioChecked()" ... />

function isRadioChecked() {
    eleRadios = document.getElementsByTagName("input");
    for (var i=0; i<eleRadios.length; i++) {
        if (eleRadios.type == 'radio' && eleRadios.checked)
           return true;    
    }
   alert("please make a selection");
   return false;
}

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial