I am using jquery validation and to show the error messages using the bootstrap tooltips. This works fine, but when I insert an incorrect value in a not required field, and then delete the text and leave the field empty, the highlight effect in the input disappears but the tooltip remains visible.
JS
$('#myform').validate({ // initialize the plugin
onkeyup: false,
rules: {
field1: {
required: true,
number: true
},
field2: {
number: true
}
},
errorPlacement: function (error, element) {
var lastError = $(element).data('lastError'),
newError = $(error).text();
$(element).data('lastError', newError);
if (newError !== '' && newError !== lastError) {
$(element).tooltip({
placement: "bottom",
trigger: "manual"
}).attr('title', newError).tooltip('fixTitle').tooltip('show');
}
},
success: function (label, element) {
$(element).tooltip('hide');
}
});
HTML
<form id="myform">
<input type="text" name="field1" />
<input type="text" name="field2" />
<input type="submit" />
</form>
To solve my problem I try to use unhighlight method, but only works the first time, then the tooltip is no longer displayed when re-insert an incorrect value in the same field
unhighlight: function(element, errorClass, validClass) {
$(element).removeClass(errorClass).addClass(validClass);
$(element).tooltip('hide');
}
Type text in the 2nd field and than delete it. jsfiddle
How can I solve my problem? Thanks