Normally radiobuttons are grouped by name:
<form name="frm1">
<input type="radio" name="sex" value="male" checked="checked" /> Male<br />
<input type="radio" name="sex" value="female" /> Female
</form>
In this case you can only select male or female, but not both. Notice the checked attribute which allows us to set a default value. Returning no choice is now not possible.
In the following example I have added one group with a yes/no (radio1) and two seperate radiobuttons (radio2 and radio3).
Because radio2 and radio3 are not grouped (by name) you can select both. Normally this is not what you want so forget this, it's just for explanation. Therefor use the same name for the radiobuttons you want to group. Also use the checked attribute to set a default value.
In the form onsubmit validate the values. By using return you can cancel the submit action. In testvalues() the backgroundcolor is colored red if validation fails. When submit is pressed, all controls are colored red. See the code for further explanation.
<html>
<head>
<script type="text/javascript"></script></head></html>
function testvalues()
{
var radio1 = document.forms["frm1"]["radio1"];
var radio2 = document.getElementsByName('radio1');
if (!document.forms["frm1"]["radio2"].checked)
document.forms["frm1"]["radio2"].style.background="red";
if (!document.forms["frm1"]["radio3"].checked)
document.forms["frm1"]["radio3"].style.background="red";
if (!radio1[0].checked && !radio1[1].checked)
{
var labelanswer = document.getElementById('labelanswer');
if (labelanswer != null)
{
labelanswer.style.background="red";
}
radio1[0].style.background="orange";
radio1[1].style.background="orange";
return false;
}
return true;
}
<body>
Question 1.<br />
<form name="frm1" önsubmit="return testvalues();">
<span id="labelanswer">Answer:
Yes<input type="radio" name="radio1" id="Radio1Yes" value="yes" />
No<input type="radio" name="radio1" id="Radio2No" value="no" /></span><br />
Yes <input type="radio" name="radio2" id="Radio2Yes" value="yes" />
No <input type="radio" name="radio3" id="Radio3No" value="no" />
<input type="submit" value="Submit" />
</form>
</body>