I have multiple checkbox inputs that I am trying to create validation for. At least one checkbox has to be selected for the form to submit. What I did was add a hidden input. Then with a change function, there is a join function that combines the checkbox values. Under that there is a variable that checks for the checkboxes' length.
In the jQuery validate, I have tried requiring the field and the parent radio button that produces its associated checkboxes. Then the checkbox fields I have set a min of 1.
Currently, when I hit submit, the form goes through without any of the checkboxes being selected or the parent radio buttons.
Does anyone see what I am doing wrong?
$(document).ready(function() {
var showMe = '';
/*var checkLength = '';
$('#interest').val(checkLength);
console.log(checkLength);*/
var newStatus = '';
//Telling whether new is selected
var usedStatus = '';
//Telling whether used is selected
var newSelPush = '';
var usedSelPush = '';
$('.equipmentOptionCont').change(function() {
var newSel = [];
var usedSel = [];
//Get new mower options
$("input:checkbox[name=newMowerOption]:checked").each(function() {
newSel.push($(this).val());
newSelPush = newSel.join(', ');
});
//Get used mower options
$("input:checkbox[name=usedMowerOption]:checked").each(function() {
usedSel.push($(this).val());
usedSelPush = usedSel.join(', ');
});
//Interest length creation for validation
var interestCheck = (newSelPush || usedSelPush);
//Hidden interest input value
checkLength = interestCheck.length;
console.log(checkLength);
$('[name="interestHidden"]').val(checkLength);
});
//Validation
$('#prodNotifForm').validate({
onfocusout: true,
rules: {
interestHidden: {
//required: true,
required: $('#newMowerSelect').prop('checked') == true,
min: 1
}
},
messages: {
interestHidden: {
required: "Please choose at least one interest",
min: "At least one interest needs chosen"
}
},
errorPlacement: function(error, element) {
if ($(element).attr('name') == 'interestHidden') {
error.insertBefore(element.parent('div'));
} else {
error.insertAfter(element); // <- default
}
},
submitHandler: function(form) {
//Variables for the form ids
var notifFirstName = $('#notifFirstName').val();
$.ajax({
url: 'subscriberNotifSend.php',
type: 'POST',
data: {
'notif_first_name': notifFirstName,
},
success: function(data) {
//console.log(data); // data object will return the response when status code is
},
complete: function() {
},
error: function(xhr, textStatus, errorThrown) {
alert(textStatus + '|' + errorThrown);
//console.log('error'); //otherwise error if status code is other than 200.
}
});
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.js"></script>
<form id="prodNotifForm">
<div id="interestContainer">
<input type="radio" name="notifymethod" id="newMowerSelect" class="equipmentMainSel notifCheck">
<label for="newMowerSelect" class="checkMower">
<div class="labelMowerSelect">New</div>
<img src="" alt="New Mower Options" class="mowerOptionImgs">
</label>
<input type="radio" name="notifymethod" id="usedMowerSelect" class="equipmentMainSel notifCheck">
<label for="usedMowerSelect" class="checkMower">
<div class="labelMowerSelect">Used</div>
<img src="" alt="Used Mower Options" class="mowerOptionImgs">
</label>
<div id="interestError"></div>
<div id="newMowerOptions" class="equipmentOptionCont">
<p class="notifQuestion">New Mower Options</p>
<div class="equipmentWrap">
<label class="equipmentOptions">Ferris</label>
<input type="checkbox" name="newMowerOption" value="Ferris">
</div>
</div>
<div id="usedMowerOptions" class="equipmentOptionCont">
<p class="notifQuestion">Used Mower Options</p>
<div class="equipmentWrap">
<label class="equipmentOptions">Ferris</label>
<input type="checkbox" name="usedMowerOption" value="Ferris">
</div>
</div>
<div><input type="hidden" id="interestHidden" name="interestHidden" value=""></div>
</div>
<button id="notifSubmit">Sign up</button>
</form>