Created
April 3, 2013 19:55
-
-
Save jeffreytgilbert/5304682 to your computer and use it in GitHub Desktop.
Example form handling for jquery for handling submissions from multiple forms on a page depending on which was submitted. The reason you want to catch the submission event is because people can click "enter" and a text input and it will submit the form regardless of your button type.
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
$('form').submit(function(event){ | |
event.preventDefault(); | |
event.stopPropogation(); // same thing | |
$(this); // <-- the form that was submitted | |
$('input', this); // <-- all the inputs for this form that need validation | |
$('input', this).each(function(){ | |
var element = $(this); // <-- this is the current field which can be validated | |
if(element.attr('required') == 'required' && element.val().trim() != '' && element.val() ){ | |
// success thing | |
} else { | |
// error thing | |
} | |
}) | |
return false; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment