Created
October 5, 2016 13:11
-
-
Save townivan/2d2c610675ed196b5da5c0fb739c9747 to your computer and use it in GitHub Desktop.
html5 validation for 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
/* .invalid class prevents CSS from automatically applying */ | |
.invalid input:required:invalid { | |
background: #BE4C54; | |
} | |
.invalid textarea:required:invalid { | |
background: #BE4C54; | |
} | |
.invalid select:required:invalid { | |
background: #BE4C54; | |
} | |
/* Mark valid inputs during .invalid state */ | |
.invalid input:required:valid { | |
background: #17D654 ; | |
} | |
.invalid textarea:required:valid { | |
background: #17D654 ; | |
} | |
.invalid select:required:valid { | |
background: #17D654 ; | |
} |
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
$(function () { | |
if (hasHtml5Validation()) { | |
$('.validate-form').submit(function (e) { | |
if (!this.checkValidity()) { | |
// Prevent default stops form from firing | |
e.preventDefault(); | |
$(this).addClass('invalid'); | |
$('#status').html('invalid'); | |
} | |
else { | |
$(this).removeClass('invalid'); | |
$('#status').html('submitted'); | |
} | |
}); | |
} | |
}); | |
function hasHtml5Validation () { | |
return typeof document.createElement('input').checkValidity === 'function'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment