2015-04-05 52 views
0

我使用Javascript驗證了更改密碼的形式。我想驗證用戶使用Ajax的當前密碼是否返回true或false。如果是,則繼續檢查下一個輸入。我想我需要一個回調,但是我做錯了什麼,因爲它沒有等待Ajax結果,它還在繼續。我在哪裏以及如何放置回調? Thx太多了!表單驗證需要回調

我有一個表格:

<form id="pwdform" action="#" onSubmit="return validatePwdForm()" method="POST"> 
    <label>Current password</label> 
    <label style="display:none">Your current password is wrong</label> 
    <input type="password" id="currentpassword" name="currentpassword" value=""/> 
     ... 
</form> 

我的JavaScript驗證功能:

function validatePwdForm(){ 
    // ... checking stuff ... 
    checkPwd('givenpassword');//Ajax call (I have it working properly, returning true or false) ... 
    //... If it's false, I want to stop checking other inputs 
    //... more stuff ... 
} 

和AJAX調用與數據庫查詢密碼:

function checkPwd(pwd){ 
    //...Ajax call ok and returns true or false 
} 
+0

請問您可以添加'validatePwdForm'和'checkPwd'的代碼嗎? – Gabin 2015-04-05 09:28:51

回答

0

在你的函數validatePwdForm() ,函數checkPwd()返回的數據不會被分配給任何v ariable.Something這樣的:

var res=checkPwd(pswd); 

if(res==true){ 

       //continue checking 

      } 
else { 

    //show error message 

    } 

否則,你可以將剩餘投在其他功能,並呼籲從阿賈克斯成功函數本身的功能。

$.ajax(function(){ 
.... 
.... 
success:function(data){ 

if(data==true) separateFun(); 

else //show error message 

} 
});