2012-01-02 54 views
1

我想在調用將記錄插入數據庫的函數之前驗證我的表單。如何在我的Ajax調用中調用多個驗證函數?

 $(document).ready(function() { 
     $("#btnSignup").click(function() { 
      ///////////CAN I PUT SOMETHING HERE TO HANDLE VALIDATION? 
      $.ajax({ 
       type: "POST", 
       dataType: 'json', 
       url: "/Newsletter/Signup", 
       data: $('#signupForm').serialize(), 
       success: function (response) { 
        if (response.success) { 
         $('#signupMessage').show(0); 
        } 
        else { 
         showValidationErrors(response.Data); 
        } 
       } 
      }); 
      return false; 
     }); 

是否有可能在此處插入驗證我的表單的內容,然後如果驗證,繼續調用其餘代碼?

回答

0

是的,你當然可以把任何函數放在ajax調用之前。

$(document).ready(function() { 
    $("#btnSignup").click(function() { 
     //create a function which will do the form validation and then call it here. 
     if(form_valid()) // This will do the form validation and then only send the ajax call 
     $.ajax({ 
      type: "POST", 
      dataType: 'json', 
      url: "/Newsletter/Signup", 
      data: $('#signupForm').serialize(), 
      success: function (response) { 
       if (response.success) { 
        $('#signupMessage').show(0); 
       } 
       else { 
        showValidationErrors(response.Data); 
       } 
      } 
     }); 
     return false; 
    }); 
+0

謝謝你的寫作。非常感謝你的幫助。 – user1125648 2012-01-02 20:42:55

相關問題