Skip to content

Instantly share code, notes, and snippets.

@jeffreytgilbert
Created April 3, 2013 19:55
Show Gist options
  • Save jeffreytgilbert/5304682 to your computer and use it in GitHub Desktop.
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.
$('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