Jquery Validation Plugin with multiple forms

I love using the Jquery Validation plugin on my html forms. It makes frontend validation very easy and painless. However, today I noticed a strange problem, when I was pressing the submit button on a form, it was requiring fields to be filled out on a different form on the page. Thankfully the fix was easy. I was using the following code which worked fine for a single form:

$('form.validateMe').validate();

However, apparently if you have more than one form on the same page that requires validation, the validation plugin does not handle the jquery collection correctly. However, that can easily be fixed by using the each() method:

$('form.validateMe').each(function(){
    $(this).validate();
});

This second method will work properly with multiple forms on a single page.

Thanks to Joern Zaefferer for information on the fix.

This entry was posted in Html, JavaScript, jQuery, Plugin and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>