Last active
December 5, 2017 00:18
-
-
Save sarkarshuvojit/697958ab85176b1a07af73ed9d7405de to your computer and use it in GitHub Desktop.
Art of Laziness — Form Validation JS
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
$(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