Hi All,
I have a set of radio buttons and have a validation in javascript which checks at least one of the radio button is checked.
Code:<script language="javascript"> 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; } } 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 } </script>This works fine.Code:<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>
But If my form has only one radion button then this code doesn't work. Like this form has only one radio button in form
Could anyone provide me solution for this? Any help would be highly appreciated. Thanks in advance!!Code:<form name="myform"> <input type="radio" value="1st value" name="myradiobutton" />1st<br /> <input type="submit" name="submitit" onclick="valbutton(myform);return false;" value="Validate" /> <input type="reset" name="reset" value="Clear" /> </form>
Best Regards,
Inderpal Singh







Bookmarks