Last active
August 29, 2015 13:57
-
-
Save labithiotis/9483717 to your computer and use it in GitHub Desktop.
Set custom HTML5 validation messages on elements and reset after user input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** ============ 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