Skip to content

Instantly share code, notes, and snippets.

@FabianSchmick
Last active August 2, 2018 12:32
Show Gist options
  • Save FabianSchmick/55555f2e4a9e9827d7439c41e47ae996 to your computer and use it in GitHub Desktop.
Save FabianSchmick/55555f2e4a9e9827d7439c41e47ae996 to your computer and use it in GitHub Desktop.
HTML5 constraint validation with jQuery
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