I'd like to use the Tooltipster plugin to display form errors generated by the jQuery Validate plugin.
jQuery for Validate plugin:
$(document).ready(function () {
$('#myform').validate({ // initialize jQuery Validate
// other options,
rules: {
field1: {
required: true,
email: true
},
field2: {
required: true,
minlength: 5
}
}
});
});
jQuery for Tooltipster plugin:
$(document).ready(function () {
$('.tooltip').tooltipster({ /* options */ }); // initialize tooltipster
});
HTML:
<form id="myform">
<input type="text" name="field1" />
<input type="text" name="field2" />
<br/>
<input type="submit" />
</form>
How would I integrate the usage of these two jQuery plugins so that validation errors appear inside the tooltips?
Sparky's answer has been a staple on my site's validation, but upgrading to Tooltipster 4 required some changes. Here's how I got mine working, and with a few improvements.
On your pages, include the tooltipster css and a theme css in the head section (and any theme css that you subclass their themes with as described on its page).
Additionally, you need to include tooltipster's javascript file. You'll also need the jquery-validation javascript.
Now, in either another javascript file or in some script tags, you'll need to put your validation code as well as your tooltipster code. Here's one from a webpage I have, the changes from Sparky's answer are at the top.
Now when you type something into a field that violates one of the listed rules, a popup comes up above the box saying what jquery-validate says is wrong. It also won't open a message if there is nothing in it to show the user (which would lead to it opening a blank tooltip and then immediately closing, which looks weird).
Solutions for Tooltipster versions 2.1, 3.0, & 4.0 are included in this answer.
NOTE that
.tooltipster()
is only attached to atype="text"
input
element in my examples below. If yourform
contains other kinds of data input elements such astype="radio"
,type="date"
,textarea
,select
, etc., then you must adjust your selector accordingly or create additional instances of.tooltipster()
for these other elements. Otherwise, everything will fail with errors.Tooltipster v2.1
First, initialize the Tooltipster plugin (with any options) on all specific
form
elements that will display errors:Second, use Tooltipster's advanced options along with the
success:
anderrorPlacement:
callback functions built into the Validate plugin to automatically show and hide the tooltips as follows:Working Demo: http://jsfiddle.net/2DUX2
EDIT: Updated code to take advantage of the new Tooltipster API features released in version 2.1 on 2/12/13
UPDATE for Tooltipster v3.0
Tooltipster 3.0 is out and as such doesn't work the same as illustrated above. The following code provides an updated example. This version has the added benefit of not calling Tooltipster on every keystroke when the message has not yet changed.
(Don't forget that you still need to attach
.tooltipster()
to your relevantinput
elements as illustrated above.)DEMO using Tooltipster v3.0: jsfiddle.net/2DUX2/3/
UPDATE for Tooltipster v4.0
Note that the
onlyOne
option has been removed from the 4.0 version of the Tooltipster plugin.I also replaced the
success
callback withunhighlight
. On "optional" fields, the error message was never removed when the field was subsequently blanked out... this is because thesuccess
function does not fire under these circumstances. This issue is not isolated to Tooltipster version 4, however the following code solves it moving forward.DEMO using Tooltipster v4.0: https://jsfiddle.net/h5grrrr9/