Radio button validation using javascript.

<html>
<head>
  <script type="text/javascript">
    function radioValidation(elementID,elementName)
    {
     var selected = "";
     var radioObj = eval("document.forms[0]."+elementID);
     for(var count=0;count<radioObj.length;count++){
       if(radioObj[count].checked) {
         selected = radioObj[count].value;
         break;
       }
     }
     if(selected == ""){
        alert("Please selected your "+elementName);
        return false; 
     }
     alert("You have selected: "+selected);    
    }
  </script>
</head>
<body>
  <form>
    <input type="radio" name="sex" value="male">Male</input></br>
    <input type="radio" name="sex" value="female">FeMale</input></br>
    <input type="button" value="Check" onClick="javascript:radioValidation('sex','Sex')"></input>
  </form>
</body>
</html>

Comments

Popular Posts