JavaScript validation for Radio Buttons?
>> Saturday, June 18, 2011
RADIO BUTTON VALIDATION
<html>
<title>RadioButtons Validation</title>
<head>
<script type="text/javascript">
function valbutton(thisform) {
// place any other field validations that you require here
// validate myradiobuttons
myOption = 0;
for (i=thisform.myradiobutton.length-1; i > 0; i--) {
if (thisform.myradiobutton[i].checked) {
myOption = i; i = 0;
}
}
if (myOption == 0) {
alert("You must select a radio button");
return false;
}
// place any other field validations that you require here
//thisform.submit(); // this line submits the form after validation
}
</script>
</head>
<body>
<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>
</body>
</html>

0 comments:
Post a Comment