5

I have a form with jquery validation .After submitting the form I load the same form through Ajax . My problem is jquery validation is not working for ajax loaded form . I have tried some answers of the stack overflow but unfortunate not working .

Thanks Az

jai
  • 547
  • 7
  • 20

3 Answers3

5

use on function. .

example:

 $(document.body).on('click', '.submitBtn', function(){
       $("#form").validate({
          submitHandler: function() {
              // do anything
         } 
    });
 });
shilpi Singh
  • 402
  • 1
  • 5
  • 15
1

From the description it looks like the new DOM elements are giving you the problem. This is a very common problem. What you need to do is - if you validation is running on a button's click function then change that from .click to .live function. = http://api.jquery.com/live/

This function also validates the new DOM elements. The click function will only work on those elements which were present in the initial DOM.

If you are still having problem, share the code.

Amitav Roy
  • 741
  • 1
  • 11
  • 21
0

After you load the new form into the DOM, you need to reinitialize the validation plugin on it, since it's a different DOM element from the one that you previously loaded. You need to do this in the AJAX callback function that loads the new HTML into the DOM.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Hi Barmar ,As i am not good at JS please tell me how is it possible to reinitialize the validation plugin.You mean reload the plugin JS – jai May 16 '13 at 05:28
  • Call `$().validate({ options ...})` in the callback function. – Barmar May 16 '13 at 05:29