2013-04-09 57 views
2

此問題不是關於安全性或「如何正確實施」相關問題,而是有關預先檢查輸入值的概念。「更改密碼」對話框的正確javascript預檢方法

作爲第一種方法,我試圖執行以下程序(注意,在每個check if聲明,方法如有虛假,否則算法繼續返回一個標識符值)

Save entered values in oldPW, newPW1, newPW2 
check if oldPW is correct (Ajax request) 
    check if newPW1 == newPW2 
     check if length of newPW1 is >=6 
      check if newPW1 contains legal characters 
       check if newPW1 contains required characters (like at least one uppercase char etc.) 

這種做法造成的問題,我認爲它涉及到異步ajax請求。我返回一個特定的標識符整數,並調用方法如

switch(checkPasswords(oldPW, newPW1, newPW2){ 
    case 0: 
     // alert something suitable 
     break; 
    case 1: 
     // alert something suitable 
     break; 

    //(...) 

    case n-1: 
     // alert something suitable 
     break; 
    case n: 
     // everything is ok, change password 
     break; 
} 

在最後檢查舊密碼是否更清晰?甚至直接嘗試更改密碼時(假定新的所需密碼滿足要求),並因此只有一個ajax請求?什麼是共同的概念?

回答

1

我個人建議通過在用戶離開舊密碼輸入(onblur)時檢查舊密碼並具有自己的處理程序/成功功能以及第二個函數來處理密碼的實際更改來分離Ajax請求。

給出以下標記:

<input type="text" id="oldPassword" value="" /> 
<input type="text" id="newPassword" name="newPassword" value="" /> 
<input type="text" id="confirmNewPassword" name="confirmNewPassword" value="" /> 
<input type="button" id="submitButton" value="Reset Password" /> 

和這個(僞碼)JS:

<script type="text/javascript"> 
//some doc ready-ish function 
$('#oldPassword').blur(function(){ 
//ajax request to validate the pw 
//trigger some success/failure message 
//if failure, disable your #submitButton by unbinding the click event or something to that effect 

//if its successful, make sure you're submit button event is bound again 
}); 

$('#submitButton').click(function(){ 
//perform new pw validation 
//submit your reset password request 
}); 

</script>