This seemed like something simple, but I am stumped. I have a form and want a field to be required if the value of another field is greater than zero. While the validation actually works and allows me to submit, the error tooltip doesn't clear when I change the value back to zero.
In the fiddle, if the Misc amount is greater than 0, i want the comments to be required, otherwise not required. As I said, the validation works, but the validation message doesn't get cleared.
jQuery(document).ready(function() {
//Initialize the tooltips
jQuery('.tooltip').tooltipster({
contentAsHTML: true
});
jQuery('#frmForm :input').each(function() {
var tipelement = getTipContainer(this);
jQuery(tipelement).tooltipster({
trigger: 'custom',
onlyOne: false,
position: 'right',
multiple: true,
autoClose: true
});
});
jQuery("#frmForm").validate({
ignore: [],
rules: {
MISCComments: {
required: function(element) {
return jQuery("#MISCAmount").val() > 0;
}
}
},
messages: {
MISCComments: "Explain what this payment is for."
},
errorPlacement: function(error, element) {
var $element = jQuery(element),
tipelement = element,
errtxt = jQuery(error).text(),
last_error = '';
tipelement = getTipContainer(element);
last_error = jQuery(tipelement).data('last_error');
jQuery(tipelement).data('last_error', errtxt);
if (errtxt !== '' && errtxt != last_error) {
jQuery(tipelement).tooltipster('content', errtxt);
jQuery(tipelement).tooltipster('show');
}
},
success: function(label, element) {
var tipelement = getTipContainer(element);
jQuery(tipelement).tooltipster('hide');
}
});
//this function selects a container for 'group' elements like
//check box /radio groups
function getTipContainer(element) {
var tipelement = element;
if (jQuery(element).is(":checkbox") || jQuery(element).is(":radio")) {
tipelement = jQuery(element).parents('.container').get(0);
}
return tipelement;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tooltipster/3.3.0/js/jquery.tooltipster.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.17.0/dist/jquery.validate.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/tooltipster/3.3.0/css/tooltipster.min.css" rel="stylesheet"/>
<form name="frmForm" id="frmForm" method="post">
<div class="form-group row">
<div class="col-md-2">
CE Vendor Fees
<input type="hidden" name="CE_VENDORS" id="CE_VENDORS" value="CE_VENDORS">
</div>
<div class="col-md-3">
<input id="CE_VENDORSInvoiceNumber" name="CE_VENDORSInvoiceNumber" type="text" placeholder="Invoice #" class="form-control input-md" style="20px;" value="">
</div>
<div class="col-md-3">
<input id="CE_VENDORSAmount" name="CE_VENDORSAmount" type="text" placeholder="Amount" class="form-control input-md Amounts" value="2.00" style="text-align: right;">
</div>
</div>
<div class="form-group row">
<div class="col-md-2">
</div>
<div class="col-lg-9">
<input id="CE_VENDORSComments" name="CE_VENDORSComments" type="text" placeholder="Comments" class="form-control input-md" value="">
<label for="CE_VENDORSComments" class="error"></label>
</div>
</div>
<div class="form-group row">
<div class="col-md-12">
</div>
</div>
<div class="form-group row">
<div class="col-md-2">
Miscellaneous
<input type="hidden" name="MISC" id="MISC" value="MISC">
</div>
<div class="col-md-3">
<input id="MISCInvoiceNumber" name="MISCInvoiceNumber" type="text" placeholder="Invoice #" class="form-control input-md" style="20px;" value="">
</div>
<div class="col-md-3">
<input id="MISCAmount" name="MISCAmount" type="text" placeholder="Amount" class="form-control input-md Amounts" value="1.00" style="text-align: right;">
</div>
</div>
<div class="form-group row">
<div class="col-md-2">
</div>
<div class="col-lg-9">
<input id="MISCComments" name="MISCComments" type="text" placeholder="Comments" class="form-control input-md" value="">
</div>
</div>
<button class="button-tooltip" title="Submit details">Submit</button>
</form>