Form Validation radio

A simple javascript technique that uses the name attribute of the html document to get Element value(s) is used for validating the form ...

(You can check similar examples of form validation in the Javascript section)

 

 



<html>

<script type="text/javascript">

function valRadio(){

if ( (document.bOtfOrm.gender[0].checked == false )

&& document.bOtfOrm.gender[1].checked == false) {

alert ( "Please choose your gender." ); 

//checked is the property of the element checkbox... 

return false; }

}

</script>

<body> 

<form name="bOtfOrm" action="some.php" onSubmit="return valRadio()">  

Gender

Male<input type="radio" name="gender" value="male"/><br/>

Female<input type="radio" name="gender" value="male"/>

<!--if we use the same name for multiple elements in a form, it will become an array.See javascript how do we use the array-->

<br/><br/>

<input type="submit" value="submit"/>

</form>

</html>

A demo of the above example is given below -  

 

Gender Male
Female