radio button validation in javascript – How can I check whether a radio button is selected with JavaScript?

In this tutorial you can learn radio button validation in javascript for i.e checked or unchecked using JavaScript Get the checked property of the radio button.

radio button validation in javascript

here you can learn to main 3 examples like javascript radio button validation, gender validation in javascript and radio button validation in javascript.

RadioButton Validation

index.html

<html>
  <head>
 // Add script tag here
  function checkFormIsValid(form){
  displayErr= "";
  if ( ( form.gender[0].checked == false ) && ( form.gender[1].checked == false ) ) 
  {
	  alert ( "Please select your Gender: Male or Female" ); 
	  return false;
  }
  if (displayErr= "") { form.submit() }
  }
 // Close script tag here 
 </head>
  <body>
 <form name="comment" 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="checkFormIsValid(this.form)">
 <input type="reset" value="Reset">
  </form> 
  </body>
  </html>

don’t miss : Radio button validation in javascript for gender

Also Read This πŸ‘‰   PHP Toggle Like Dislike Rating System using jQuery Ajax

How to check whether a radio button is selected with JavaScript?

if ($('input[name=gender]:checked').length > 0) {
    // do something here
}

javascript radio button validation

Validate Group of RadioButtons (Multiple RadioButtons) using JavaScript Example
index.html

<html>
<head>
<script type="text/javascript">
function selectPlayer()
{
	var team = document.comment.player;
	for (var i=0; i<team.length; i++) {
	if (team[i].checked)
	break;
	}
	if (i==team.length)
	return alert("Sorry, No radio button is checked");
	alert("Radio Button " + (i+1) + " is checked.");
}
</script>
</head>
<body>
<form name="comment">
Radio Button 1: <input type="radio" name="player" /><br />
Radio Button 2: <input type="radio" name="player" /><br />
Radio Button 3: <input type="radio" name="player" /><br />
Radio Button 4: <input type="radio" name="player" /><br /><br />
<input type="button" value="Eval Group" onclick="selectPlayer()" />
</form>
</body>
</html>

Free Live Chat for Any Issue

gender validation in javascript

function validate(form)
{
    var genderM=form.gender_male;
    var genderF=form.gender_female;

    if(genderM.checked==false && genderF.checked==false ) {
        alert("You must select male or female");
        return false;
    }   
}

See example

Also Read This πŸ‘‰   Codeigniter Form Validation with Google reCAPTCHA

<form name="contact">
  <label> <input type="radio" name="gender"> : Male</label>    
  <label> <input type="radio" name="gender"> : Female</label>    
  <button type="reset">Reset</button>
  <button>Submit</button>
</form>

<script>
document.contact.onsubmit=function(){
    if(this.gender[0].checked==false && this.gender[1].checked==false){
        alert("Select Gender");
        return false;
    }
    else{
        alert("Great!!!! ");
    }
}
</script>

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