Radio Buttons:
[html]<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>[/html]
Javacript:
- 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
-
}
Checkboxes are pretty much the same way.
HTH,
Aric