Created
October 28, 2012 04:06
-
-
Save ianthrive/3967461 to your computer and use it in GitHub Desktop.
Make HTML5 form validation appear the same way in all recent browsers as Safari
This file contains 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
window.addEventListener('load', function(event) { | |
// for each form on the page | |
for(i=0; i < document.forms.length; i++) { | |
f = document.forms[i]; | |
// add a 'submit' event listener | |
f.noValidate = true; | |
// except for safari, the event never triggers without noValidate=true | |
f.addEventListener('submit', function(event) { | |
// if form is not valid don't let it submit | |
if(!event.target.checkValidity()) { | |
event.preventDefault(); | |
alert('Form is not valid.'); | |
} | |
}, false); | |
} | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment