how to set radio button checked validation in javascript?

Today, We want to share with you radio button validation in javascript.In this post we will show you HTML DOM Input Radio checked Property, hear for how to set radio button checked in javascript we will give you demo and example for implement.In this post, we will learn about Get Selected Radio Button Value Using JQuery with an example.

RADIO BUTTON VALIDATION USING JAVASCRIPT

Example 1: index.html

<html>
<head>
<script LANGUAGE="JavaScript">
function ValidateForm(form){
ErrorText= "";
if ( ( form.gender[0].checked == false ) && ( form.gender[1].checked == false ) )
{
alert ( "Please choose your Gender: Male or Female" );
return false;
}
if (ErrorText= "") { form.submit() }
}
</script>
</head>
<body>
<form name="feedback" action="#" method=post>
Your Gender: <input type="radio" name="gender" value="Male"> Male
<input type="radio" name="gender" value="Female"> Female
<input type="button" name="SubmitButton" value="Submit" onClick="ValidateForm(this.form)">
<input type="reset" value="Reset">
</form>
</body>
</html>

Example 2: index.html

<html>
<head>
<script type="text/javascript">
function getAllProducts()
{
	var all_radio = document.productFrm.getProducts;
	for (var i=0; i<all_radio.length; i++) {
		if (all_radio[i].checked)
		break;
	}

	if (i==all_radio.length)
	return alert("Sorry, No radio button is checked");
	alert("Valuable Product " + (i+1) + " is checked.");
}
</script>
</head>
<body>
<form name="productFrm">
Valuable Product 1: <input type="radio" name="getProducts" /><br />
Valuable Product 2: <input type="radio" name="getProducts" /><br />
Valuable Product 3: <input type="radio" name="getProducts" /><br />
Valuable Product 4: <input type="radio" name="getProducts" /><br /><br />
<input type="button" value="Eval Group" onclick="getAllProducts()" />
</form>
</body>
</html>

Example 3: index.html

<html>
  <head>
 // Add script tag here
  function ValidateForm(form){
  ErrorText= "";
  if ( ( form.gender[0].checked == false ) && ( form.gender[1].checked == false ) ) 
  {
  alert ( "Please Select your Gender: Male or Female" ); 
  return false;
  }
  if (ErrorText= "") { form.submit() }
  }
 // Close script tag here 
 </head>
  <body>
 <form name="feedback" action="#" method=post>
   Gender: 
     <input type="radio" name="gender" value="Male"> Male
  <input type="radio" name="gender" value="Female"> Female
  
    
    <input type="submit" name="SubmitButton" value="Submit" onClick="ValidateForm(this.form)">
 <input type="reset" value="Reset">
  </form> 
  </body>
  </html>

I hope you get an idea about How to check a radio button using JavaScript?.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.