Skip to content

Instantly share code, notes, and snippets.

@ssajous
Created July 15, 2013 14:26
Show Gist options
  • Save ssajous/6000374 to your computer and use it in GitHub Desktop.
Save ssajous/6000374 to your computer and use it in GitHub Desktop.
Validation utility to be used with Twitter Bootstrap. Displays tooltip on the first invalid element in the form, as well as highlights all invalid elements.
var ValidationUtility = function() {
var validationElements = $('[data-role="validate"]');
var elementCount;
validationElements.popover({
placement: 'top'
});
validationElements.on('invalid', function() {
if (elementCount === 0) {
$('#' + this.id).popover('show');
elementCount++;
}
});
validationElements.on('blur', function() {
$('#' + this.id).popover('hide');
});
var validate = function(formSelector) {
elementCount = 0;
if (formSelector.indexOf('#') === -1) {
formSelector = '#' + formSelector;
}
return $(formSelector)[0].checkValidity();
};
return {
validate: validate
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment