So you want to put radio buttons on a form on your page but don't know how to go about validating that one has been selected or determining which one has been selected.
Well here we have a form containing three radio buttons to show you how to do it.
( )1st
( )2nd
( )3rd
[Validate] [Clear]
( )2nd
( )3rd
[Validate] [Clear]
<form name="myform">
<input type="radio" value="1st value" name="myradiobutton" />1st<br />
<input type="radio" value="2nd value" name="myradiobutton" />2nd<br />
<input type="radio" value="3rd value" name="myradiobutton" />3rd<br /> <br />
<input type="submit" name="submitit" onclick="valbutton(myform);return false;" value="Validate" />
<input type="reset" name="reset" value="Clear" />
</form>
<input type="radio" value="1st value" name="myradiobutton" />1st<br />
<input type="radio" value="2nd value" name="myradiobutton" />2nd<br />
<input type="radio" value="3rd value" name="myradiobutton" />3rd<br /> <br />
<input type="submit" name="submitit" onclick="valbutton(myform);return false;" value="Validate" />
<input type="reset" name="reset" value="Clear" />
</form>
function valbutton(thisform) { // place any other field validations that you require here // validate myradiobuttons myOption = -1; for (i=thisform.myradiobutton.length-1; i > -1; i--) {
if (thisform.myradiobutton[i].checked) { myOption = i; i = -1; } } if (myOption == -1) { alert("You must select a radio button"); return false; } alert("You selected button number " + myOption
+ " which has a value of " + thisform.myradiobutton[myOption].value); // place any other field validations that you require here thisform.submit(); // this line submits the form after validation
}
No comments:
Post a Comment