2016-02-03 46 views
0

我期待在AJAX中轉換我的腳本的一部分,因爲它不會像Synchronous JAX那樣凍結頁面。我想要實現的是在將密碼發送到服務器之前驗證密碼是否正確。如果不是,密碼字段將抖動並顯示錯誤消息。從SJAX到AJAX

<script type="text/javascript"> 
    var ok = null; 
    var lastChanged = ""; 
     $(document).ready(function() { 
      $.localScroll({duration:800}); 
      <?php if ($wrong_pass): ?> 
       $("form#password_box").velocity("callout.shake"); 
      <?php endif; ?> 
      $("[name=pass]").on("keyup change propertychange", onPassChange); 
     }); 
    </script> 
    <script type="text/javascript"> 
     function onPassChange(e) { 
      // get keycode of current keypress event 
      var code = (e.keyCode || e.which); 
      // do nothing if it's the enter key 
      if(code != 13) { 
       var init_val = $("#password_field").val(); 
       $.ajax({type: "POST", url:"check_password_AJAX", data:{pass:$("#password_field").val()}, dataType:"text", success: function (data) { 
        if (data && $("#password_field").val() == init_val) { 
         if (data == "TRUE") { 
          ok = true; 
          lastChanged = init_val; 
         }else { 
          ok = false; 
          lastChanged = init_val; 
         } 
        } 
       }}); 
      } 
     } 
    </script> 
    <script type="text/javascript"> 
     function validateForm() { 
      if (ok && lastChanged == $("#password_field").val()) { 
       $("#incorrect").velocity("transition.fadeOut"); 
       $("#incorrect").html(""); 
       return true; 
      }else if (lastChanged != $("#password_field").val()) { 
       //ajax 
       var obj = $.ajax({type: "POST", url:"check_password_AJAX", data:{pass:$("#password_field").val()}, dataType:"text", async:false}); 
       // Analyse 
       if (obj.responseText) { 
        if (obj.responseText == "TRUE") { 
         $("#incorrect").velocity("transition.fadeOut"); 
         $("#incorrect").html(""); 
         return true; 
        }else { 
         $("#password_container").velocity("callout.shake"); 
         $("#incorrect").html("Mot de passe incorrect"); 
         $("#incorrect").velocity("transition.fadeIn"); 
         $("#password_field").val(""); 
         return false; 
        } 
       } 
      }else { 
       $("#password_container").velocity("callout.shake"); 
       $("#incorrect").html("Mot de passe incorrect"); 
       $("#incorrect").velocity("transition.fadeIn"); 
       $("#password_field").val(""); 
       return false; 
      } 
     } 
</script> 
<form action="cmd" method="post" class="center" id="password_box" onsubmit="return validateForm()"> 
            <div id="password_container"> 
             <input type="password" name="pass" id="password_field" placeholder="Mot de passe"><br> 
            </div> 
            <input type="submit" name="submit" class="button" value="S'autentifier"> 
          </form> 

對於每一個按鍵,我去嘗試驗證使用AJAX(onPassChange功能)的密碼,並把結果在ok變量。如果用戶在使用AJAX驗證之前嘗試提交表單,我發現的唯一方法是使用SJAX。謝謝。

回答

0

我想你誤解了AJAX的用法。 AJAX用於執行異步HTTP(Ajax)請求。在客戶端驗證(我假設你的意思是正確的格式,並且不是正確的密碼)不應該使用AJAX完成。將用戶名和密碼發送到後端服務器可以使用AJAX完成,因爲它是HTTP請求。如果您在用戶輸入文本時嘗試檢查密碼是否正確,我會有興趣瞭解該方法適合的用例。

編輯:我做了快速和簡單的小提琴來演示在客戶端上進行密碼格式驗證。注意onPassChange函數中的更改以及在id爲passwordInvalid的html中添加元素。該通知僅消失如果「格式」是密碼https://jsfiddle.net/032mn56t/19/

function onPassChange(e) { 
    // get keycode of current keypress event 
    var code = (e.keyCode || e.which); 
    console.log(code); 
    // do nothing if it's the enter key 
    if(code != 13) { 
     var init_val = $("#password_field").val(); 
     //substitute the following with matching input to a regex meething your password requirements. THis is just for demonstration. 
     if(init_val !== "format"){ 
      $("#passwordInvalid").show(); 
     }else{ 
      $("#passwordInvalid").hide(); 
     } 
    } 
<form action="cmd" method="post" class="center" id="password_box" onsubmit="return validateForm()"> 
    <div id="password_container"> 
     <input type="password" name="pass" id="password_field" placeholder="Mot de passe"><br> 
    </div> 
     <input type="submit" name="submit" class="button" value="S'autentifier"> 
</form> 
<p id="passwordInvalid" style="display:none"> 
    WRONG PASSWORD FORMAT 
</p> 
+0

我知道AJAX不應該用於客戶端驗證,所以我也有一個服務器端驗證。爲什麼我使用AJAX是爲了快速向用戶反饋密碼的有效性。在將AJAX添加到組合中之前,我剛剛進行了PHP驗證,但是發現看到頁面重新加載並隨後顯示相同提示(文本字段)的用戶感到困惑。 – Bouzmine

+0

用一些簡單的代碼更新了答案,以演示如何在客戶端進行驗證。如果您有興趣使用框架進行客戶端驗證,請查看jQuery驗證插件。 –