I have a form
<form method="post" action="" onsubmit="validate()">
<input type="checkbox" name="del[]" value="1">
<input type="checkbox" name="del[]" value="2">
<input type="checkbox" name="del[]" value="3">
<input type="checkbox" name="del[]" value="4">
<input type="checkbox" name="del[]" value="5">
<button type="submit" class="btn btn-danger btn-lg">delete</button>
</form>
i try to do checkbox validation with JavaScript,if people not select a check box,it will show a message,if people select one or more than one check box, it will show the confirm alert to confirm submit.But my JavaScript is not work. The form will submit without validation.
<script>
function validate() {
var checkbox=document.getElementsByName("del[]");
if (checkbox.checked == null || x == "") {
alert("Please select!");
var check=false;
return false;
}
if(check != false && !confirm('confirm submit?')){
e.preventDefault();
return false;
}
return true;
}
</script>
How can i fix the problem?