I have a form and try to disable post it if radios not selected:
function checkForm(obj){
var return_value = true;
var error_msg = 'Some text: '+'\n';
$("input[type='radio']:not(:checked)").each(function() {
error_msg += $(this).next().text() + ", ";
});
if(!return_value)
alert(error_msg);
return return_value;
}
Form:
<form name="test1" method="post" action="result.php" onsubmit="return checkForm(this);">
<span id="q2"><h4>My question</h4></span>
<ul>
<li><input type="radio" name="answer[2]" value="5" />a1</li>
<li><input type="radio" name="answer[2]" value="6" />a2</li>
<li><input type="radio" name="answer[2]" value="7" />a3</li>
<li><input type="radio" name="answer[2]" value="8" />a4</li>
</ul>
</div>
<div><input type="submit" value="Send!" name="submit" class="button" id="start" /></div>
</form>
But no alert shows and the form is submitted.
you can try this out
and in html remove onsubmit function
You should set the
return_value
tofalse
if the radio buttons are not checked, because the initial value istrue
.