Last active
August 2, 2018 12:32
-
-
Save FabianSchmick/55555f2e4a9e9827d7439c41e47ae996 to your computer and use it in GitHub Desktop.
HTML5 constraint validation with jQuery
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
function htmlConstraintValidation(e, el) { | |
var form = $(el).closest('form'); | |
$(form).find('[required]').each(function (i, input) { | |
if (input.validity.valueMissing) { | |
console.log('Please fill in all required fields!'); | |
e.preventDefault(); | |
return false; | |
} | |
}); | |
} | |
$(document).ready(function () { | |
jQuery('body').on('click', 'button[type="submit"]', function (e) { | |
htmlConstraintValidation(e, $(this)); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment