I have three checkboxes on my page and I want at least one of the checkboxes to be required to be check on submission. I already followed below post, but this did not resolve my issue:
Submit form only if at least one checkbox is checked
someone closed my post because it is duplicate of Why does jQuery or a DOM method such as getElementById not find the element?
I am not using document.getElementById,
Below is my code:
<form asp-action="Create">
<input type="checkbox" id="Box2" name="test" />Yes
<input type="checkbox" id="Box3" name="test" />No
<input type="checkbox" id="Box3" name="test" />May be
<button value="Request" type="submit">Submit</button>
</form>
script>
$("#submit").click(function(e){
var number_of_checked_checkbox= $("input[name=test]:checked").length;
if(number_of_checked_checkbox==0){
alert("select any one");
}else{
$("button[type=submit]")
}
});
});
</script>
when I don’t check any checkbox and click on submit button, page gets posted without any error message. I have several other checkboxes on my web page, that’s why I put the “name” attribute on my checkboxes.
any help will be appreciated.