dimanche 1 juillet 2018

JavaScript alert mechanism not working

I want my script to behave such that on form submission, if none of radio button is checked, it should alert user to select one. I have written code but it isn't working. Help please.

Below is my HTML code:

 <form onSubmit="return validateRadios();">
 Select your qualification     
    Intermediate<input type="radio" name="qualification" value="inter" />
    Bachelors<input type="radio" name="qualification" value="Bchlrs"/>
    Masters<input type="radio" name="qualification" value="mstrs"/>
   </br>
   <input type="submit" value="Lock details"/>        
</form>

Below is my script:

function validateRadios() {
  var c = document.getElementsByName("qualification");
  for(var a=0;a<=c.length;a++) {
    if(c[a].checked ) {
      alert("Form OK!");
      return true;
    }
  }
  alert("Please select one");
  return false;
} 




Aucun commentaire:

Enregistrer un commentaire