jQuery

Validate radio buttons

Checkbox validation on submit for fruit selection

This jQuery snippet ensures that the user selects at least one checkbox with the name "fruit" before allowing form submission.

Demo
HTML
<section class="demo-section">
<form action="" method="POST" id="demoForm"> 
<div class="container demo-container">
<div class="demo-image"></div>

<div class="row justify-content-between">
<div class="col-md-3 col-radio"> 
<input type="radio" name="fruit" id="radio-01" value="Apple">
<label for="radio-01">Apple</label></div>

<div class="col-md-3 col-radio"> 
<input type="radio" name="fruit" id="radio-02" value="Banana">
<label for="radio-02">Banana</label></div>

<div class="col-md-3 col-radio"> 
<input type="radio" name="fruit" id="radio-03" value="Grapes">
<label for="radio-03">Grapes</label></div>

<div class="col-md-3 col-radio"> 
<input type="radio" name="fruit" id="radio-04" value="Peach">
<label for="radio-04">Peach</label> </div>

</div> 
</div>

<div id="submit-message"></div>

<div class="text-center submit-button-container">
<input class="btn btn-primary submit-button" type="submit" name="submit" id="submit" value="Submit">
</div>
</form>

</section>
CSS
.demo-container {
  padding: 2em;
  background-image: linear-gradient(to top, #ececec 0%, #ffffff 100%);
}

.demo-image {
  width: 100%;
  height: 420px;
  background-image: url("image.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
  background-size: cover;
  margin-bottom: 1em;
}

.col-radio input {
  display: none;
}

.col-radio label {
  display: block;
  position: relative;
  padding: 10px 20px 4px 40px;
  border-bottom: 1px dashed rgba(0, 0, 0, 0.30);
  color: rgba(0, 0, 0, 0.30);
  cursor: pointer;
}

.col-radio label::before {
  content: '';
  display: block;
  position: absolute;
  top: 10px;
  left: 0;
  width: 24px;
  height: 24px;
  border: 2px solid rgba(0, 0, 0, 0.30);
  border-radius: 100%;
}

.col-radio label:hover, input:focus + label {
  border-bottom: 1px dashed rgb(0, 0, 0, 1.00);
  color: rgba(0, 0, 0, 1.00);
}

.col-radio label::before:hover {
  border: 2px solid rgba(0, 0, 0, 0.60);
  color: rgba(0, 0, 0, 1.00);
}

.col-radio label:hover::before {
  border: 2px solid rgba(0, 0, 0, 0.60);
} 

.col-radio input:checked + label {
  border-bottom: 1px dashed rgb(0, 0, 0, 1.00);
  color: rgba(0, 0, 0, 0.90);
}

.col-radio input:checked + label::before {
  position: absolute; 
  padding-top: 2px;
  padding-left: 4px;
  font-family: 'FontAwesome';
  font-size: 12px;
  content: '\f00c'; 
  background: rgba(255, 255, 255, 0.80);
  border: 2px solid rgba(0, 0, 0, 0.60);
  color: rgba(0, 0, 0, 1.00);
  font-weight: bold; 
}

.input-error {
  position: relative;
  text-align: center;
  padding: 4px 20px 0 20px;
  color: rgba(154, 0, 0, 0.60);
}

.input-error::after {
  position: absolute;
  font-family: 'FontAwesome';
  font-size: 20px;
  content: "\f25a";
  top: 4px;
  right: 0;
}

.input-error::before {
  position: absolute;
  font-family: 'FontAwesome';
  font-size: 20px;
  content: "\f25a";
  top: 4px;
  left: 0;
  -moz-transform: scale(-1, 1);
  -webkit-transform: scale(-1, 1);
  -o-transform: scale(-1, 1);
  -ms-transform: scale(-1, 1);
  transform: scale(-1, 1);
}
.submit-button-container {
  margin-top: 1em;
}

.submit-button {
  width: 100%;
}

#submit-message {
  margin-top: 10px;
}

.input-submitted {
  text-align: center;
  color: rgba(0, 102, 49, 0.60)
}
JS
$(function() {

$('#submit').click(function() {

if ($('input[name="fruit"]:checked').length == 0) {
$("#submit-message").html('<div class="input-error">Please make a selection</div>');
$("#submit").addClass("btn-warning");
return false;
} 

else {
$("#submit").removeClass("btn-warning");
return true;
}

});
});