Hi, i am trying to validate my radio buttons using my for loop in my javascript, but it does not go into my for loop. <script language="JavaScript"> <!-- function validate_radio(myForm) { alert("inside function"); var radioGroup = myForm.myRadios; var radioGroupSelected = false; var radIdx; for(radIdx = 0; radIdx < radioGroup && !radioGroupSelect; radIdx++) { if (radioGroup[radIdx].checked) { radioGroupSelected = true; } else if (!radioGroupSelected) { alert("Please select an option"); } return radioGroupSelected; } alert("outside of loop") } --> </script> </head> Code (markup): <body> <center> <form> Hugh Jackman <input type="radio" name="myRadios" value="Hugh Jackman "> <br /> Tom Cruise <input type="radio" name="myRadios" value="Tom Cruise " ><br /> Halle Berry <input type="radio" name="myRadios" value="Halle Berry " ><br /> <input type="submit" onclick="return validate_radio(this.form)" /> </form> </center> </body> </html> Code (markup): Can anybody suggest any reason why? Any help would be greatful.
Untested: <script language="JavaScript"> <!-- function validate_radio(myForm) { var radioGroup = myForm.myRadios; for (var i = 0; i < radioGroup.length; i++) { if (radioGroup[i].checked) { return true; } } alert('Please select at least one.'); return false; } --> </script> Code (javascript): And add this to your form: <form onsubmit="return validate_radio(this);" method="post" action=""> Code (markup): EDIT: I just see you're not submitting the form. Take out the "form" in the onclick attribute. onclick="return validate_radio(this)" Code (markup):