Home > DeveloperSection > Forums > jQuery: how to include validation in the ajaxForm function?
Tanuj Kumar

Total Post:134

Points:940
Posted on    March-11-2013 1:35 AM

 JQuery JQuery 
Ratings:


 1 Reply(s)
 1317  View(s)
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.



AVADHESH PATEL

Total Post:604

Points:4228
Posted on    March-11-2013 8:38 AM

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*/})
  }

})

Don't want to miss updates? Please click the below button!

Follow MindStick