Skip to content

Instantly share code, notes, and snippets.

@labithiotis
Last active August 29, 2015 13:57
Show Gist options
  • Save labithiotis/9483717 to your computer and use it in GitHub Desktop.
Save labithiotis/9483717 to your computer and use it in GitHub Desktop.
Set custom HTML5 validation messages on elements and reset after user input
/** ============ SET HTML5 VALIDATION MESSAGE ============
document.getElementById('element').setValidationMessage('Oops.. that\'s not right');
=========================================================== **/
(function(window, Element){
"use strict";
Element.prototype.setValidationMessage = function(msg) {
this.setCustomValidity(msg);
function removeCustomValidity(){
this.setCustomValidity('');
this.removeEventListener('input', removeCustomValidity, false);
}
this.addEventListener('input', removeCustomValidity.bind(this), false);
}
}(window, Element));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment