Skip to content

Instantly share code, notes, and snippets.

@mazfreelance
Created April 13, 2018 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mazfreelance/73291a614f6af4778d8e9802b8a9b6ff to your computer and use it in GitHub Desktop.
Save mazfreelance/73291a614f6af4778d8e9802b8a9b6ff to your computer and use it in GitHub Desktop.
validation checkbox select atleast one
//html
<input type="checkbox" class="checkbox" name="checkbox[]" value="checkbox 1"/>
<input type="checkbox" class="checkbox" name="checkbox[]" value="checkbox 2"/>
<p id="validation"></p>
//form submit
$('#form-id').submit(function(evt){
evt.preventDefault();
...
if(!$('.checkbox').is(":checked")) {
document.getElementById("validation").innerHTML="Please select atleast one checkbox";
return false;
}
...
});
//hide error validation if user click atleast one checkbox.
$(".checkbox").click(function(){
$(this).is(':checked') ? $('#benefit_valid').hide() : $('#benefit_valid').show();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment