2

I have this in JQuery:

$("form").validate({  rules: {
            captcha: {
                required: true,
                remote: {
        url: "gb_include/captcha.php",
        type: "post"
          },
            }
        },
        messages: {
            captcha: "Correct captcha is required."
        }});
   $("form").submit(function(){

        if($(this).valid() == true){ /* submit via ajax */ }

But I have to us .live() because the form is loaded after $(document).ready() how can i do that ?

2 Answers2

2

You can create a custom event to run after the form is loaded.

$(document).bind('bindForm', function (e) {
    $("#form").validate({  rules: {
        captcha: {
            required: true,
            remote: {
    url: "gb_include/captcha.php",
    type: "post"
      },
        }
    },
    messages: {
        captcha: "Correct captcha is required."
    }});
    $("form").submit(function(){
        if($(this).valid() == true){ /* submit via ajax */ }
    });
});

Add this after the code that loads the form:

$(document).trigger('bindForm');
Jeff the Bear
  • 5,603
  • 3
  • 22
  • 18
1

Why don't you execute the above code in the .load callback?

See this SOq:

which has a similar requirement.

Community
  • 1
  • 1
icyrock.com
  • 27,952
  • 4
  • 66
  • 85
  • Because i load data with Jquery ajax '$.ajax', and the function is called on menu links and will ask for different contents so i have to POST some data to server which is not possilbe with load that easy, anyways can you give an example of how doing that with load ? –  Dec 05 '10 at 16:36
  • OK - when do you generate the `#form` element? Can you just execute the above code after you generate `#form`? As for using `.load`, take a look at the question link I posted, there's an example in the accepted answer. – icyrock.com Dec 05 '10 at 16:39
  • that is true, but there are lots of changes in page, like i have forms with an input or two, it will be more expansive does int ? –  Dec 06 '10 at 02:15
  • In your question you used `#form`, meaning you have only one form. If you have multiple forms, you can do something like in this SOq: http://stackoverflow.com/questions/2219485/jquery-validation-plugin-dynamic-form-validation, i.e. use the class like ".formThatShouldBeValidated" to select all the forms with that class using `.live`. Anyway, glad you found a solution that works for you! – icyrock.com Dec 06 '10 at 02:25