Skip to content

Instantly share code, notes, and snippets.

@sarkarshuvojit
Last active December 5, 2017 00:18
Show Gist options
  • Save sarkarshuvojit/697958ab85176b1a07af73ed9d7405de to your computer and use it in GitHub Desktop.
Save sarkarshuvojit/697958ab85176b1a07af73ed9d7405de to your computer and use it in GitHub Desktop.
Art of Laziness — Form Validation JS
$(document).ready(function(){
// invoke function when form is submit
$("#form-name").submit(function(e){
// select all the input fields with form-validate class and loop over them
$(".form-validate").each(function () {
// check if the the form is empty/filled with spaces
if($(this).val().trim().length == 0){
// get the respective data-validation-error
var errorMessage = $(this).attr('data-validation-error');
// show the error message somehow
alert(errorMessage);
// prevent the form from being submit
e.preventDefault();
// break the jQuery each loop
return false;
}
})
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment