I am using jQuery validation to validate the form. I have 3 checkboxes as of now in my form.
I have to validate only one filed and I have to display only one message above the submit button. I tried using require_from_group
and showErrors
. I am getting the validation message but when I checked the checkbox then my message is still showing here.
I tried below code
$('#contactform').on('submit', function(event) {
$('.anyOneValidation').each(function() {
$(this).rules("add", {
//required: true,
require_from_group: [1, ".anyOneValidation"],
messages: {
require_from_group: "Please select any one field."
}
})
});
// prevent default submit action
event.preventDefault();
// test if form is valid
if ($('#contactform').validate().form()) {
console.log("validates");
} else {
console.log("does not validate");
}
});
$("#contactform").validate({
showErrors: function(errorMap, errorList) {
$(".form-errors").html("Please select any one of the check box before you submit the form.");
},
});
<form method="post" name="contactform" id="contactform" autocomplete="off">
<input type="checkbox" name="checkname[1]" value="checkbox1" class="anyOneValidation"><label>box1</label>
<input type="checkbox" name="checkname[2]" value="checkbox1" class="anyOneValidation"><label>box2</label>
<input type="checkbox" name="checkname[3]" value="checkbox1" class="anyOneValidation"><label>box3</label>
<div class="form-errors"></div>
<input type="submit" name="submit" value="submit">
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/additional-methods.min.js"></script>
Would you help me out in this issue?