8

i am using ajaxForm() frame work to send my data without reloading my page.

    $('#ReplayForm').ajaxForm({ 

      success : function(data){
             alert("Success");
       }   
    });  

now, i want to check some condition before submitting the form and if the condition is false then stop submission else continue.

is there any solution to do this work or is there any way buy which i can perform this operation. Thanks in advance.

Uday A. Navapara
  • 1,237
  • 5
  • 20
  • 40

3 Answers3

10

Yes, definatly you can handle this situation. you have to call beforesubmit method for this let see one example

$('#ReplayForm').ajaxForm({ 
         beforeSubmit : function(arr, $form, options){
             if("condition is true")
             {
                return true; //it will continue your submission.
             }
             else
             {
                               return false; //ti will stop your submission.
             }

          },
          success : function(data){
              endLoading();
              if(data.result=="success")            
              {
                  showSuccessNotification(data.notification);
              } 
              else
              {
                  showErrorNotification(data.notification);
              }
           }
   });  
Anton
  • 32,245
  • 5
  • 44
  • 54
Ashish Vaghasiya
  • 327
  • 5
  • 20
3

You can use the beforeSubmit option

$('#ReplayForm').ajaxForm({
    beforeSubmit: function (arr, $form, options) {
        //check your conditions and return false to prevent the form submission
        if (!valid) {
            return false;
        }
    },
    success: function (data) {
        alert("Success");
    }
});
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
3

Use beforeSend option in JQuery AJAX framework, if the test fails return false should do it.

$('#ReplayForm').ajaxForm({ 

  success : function(data){
         alert("Success");
  },
  beforeSend: function() {
      if(!myFunc()) {
          return false;
      }
  }
});
Edward
  • 1,806
  • 5
  • 26
  • 36
  • 2
    edward you try this code in mobile browser with required field? check its not work, without check plz do not answer the question, to many developer and programmer are checking your code and dependent on you ans. – Janak Dhanani Sep 22 '14 at 09:05
  • Do you have a mobile solution Janak? – Edward Sep 22 '14 at 09:22
  • Mr. edward i am facing this mobile problem from last 1 month and not getting proper solution for ajaxForm submit without loading, its work fine but getting problem with mobile or safari browser only,(i dnt want to check every time javascript and jquery validation for required field) – Janak Dhanani Sep 22 '14 at 09:24
  • and the alternative solution that i was looking to my code is spring validation while inserting data – Janak Dhanani Sep 22 '14 at 09:25
  • Ok, well there is no need to get moody about it, why don't you post a question? – Edward Sep 22 '14 at 09:27
  • i was posted before, its fail to get proper solution, even jquery also speechless for this question...they reply me "we are fixing this bug very soon" – Janak Dhanani Sep 22 '14 at 09:31