Skip to content

Instantly share code, notes, and snippets.

@ianthrive
Created October 28, 2012 04:06
Show Gist options
  • Save ianthrive/3967461 to your computer and use it in GitHub Desktop.
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
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