JQUERY: HOW TO INCLUDE VALIDATION IN THE AJAXFORM FUNCTION?

Tanuj Kumar

Total Post:134

Points:940
Posted by  Tanuj Kumar
JQuery 
 1723  View(s)
Ratings:
Rate this:

Hi Everyone!

I have a form which needs to be validates and then submitted with ajaxForm (the form contains image and file data, so submission with .serialize() won't

work). The following are the details:

HTML:

<form id="myForm" action="..." method="post" enctype="multipart/form-data">
  ...
  <input type="file" name="image" /><br />
  <input type="file" name="file" /><br />
  ...
</form>
jQuery:

$(document).ready(function() {

  $("#myForm").ajaxForm ({

    beforeSubmit: function() {
      $("#myForm").validate({
        onkeyup:false,
        rules: {
          ...
        },
        messages: {
          ...
        },
      });
    },

    success: function(returnData) {
      $('#content').html(returnData);
    }

  });

});

Thanks in advance.

  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: jQuery: how to include validation in the ajaxForm function?

    Hi Tanuj!

    You can validate in ajaxForm function as per following!

    $("#myForm").submit(function(e){
      e.preventaDefault();

       var isValid=/* run your validation code that determines true or false*/

       if( isValid){
         $(this).ajaxForm({/* plugin options*/})
      }

    })

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!