首頁  >  文章  >  web前端  >  javascript和jquery分別實作使用者登入驗證_javascript技巧

javascript和jquery分別實作使用者登入驗證_javascript技巧

WBOY
WBOY原創
2016-05-16 15:02:271339瀏覽

在上一篇文章/article/83504.htm中,用javascript實現了用戶驗證,但並沒有對密碼進行驗證,這次追加了這個功能,並分別用javascript和jquery實現。

一.用jquery的ajax實現的關鍵程式碼

實作如下

/*jquery实现
$(document).ready(function(){
  $("#account").blur(function(event) {
    $.ajax({
      type:"GET",
      url:"checkAccount.php?account="+$("#account").val(),
      dataTypes:"text",
      success:function(msg){
        $("#accountStatus").html(msg);
      },
      error:function(jqXHR) {
        alert("账号发生错误!")
      },
    });
  });
 
  $("#password").blur(function(event) {
    $.ajax({
      type:"GET",
      url:"checkPassword.php?",
      dataTypes:"text",
      data:"account="+$("#account").val()+"&password="+$("#password").val(),
      success:function(msg){
        $("#passwordStatus").html(msg);
      },
      error:function(jqXHR) {
        alert("密码查询发生错误!")
      },
    });
  });
}); */

二.用javascript實現的關鍵程式碼

實作如下

//javascript实现
  function checkAccount(){
    var xmlhttp;
    var name = document.getElementById("account").value;
    if (window.XMLHttpRequest)
     xmlhttp=new XMLHttpRequest();
    else
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 
    xmlhttp.open("GET","checkAccount.php?account="+name,true);
    xmlhttp.send();
 
    xmlhttp.onreadystatechange=function(){
     if (xmlhttp.readyState==4 && xmlhttp.status==200)
      document.getElementById("accountStatus").innerHTML=xmlhttp.responseText;
    }
  }
 
  function checkPassword(){
    var xmlhttp;
    var name = document.getElementById("account").value;
    var pw = document.getElementById("password").value;
    if (window.XMLHttpRequest)
     xmlhttp=new XMLHttpRequest();
    else
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 
    xmlhttp.open("GET","checkPassword.php?account="+name+"&password="+pw,true);
    xmlhttp.send();
 
    xmlhttp.onreadystatechange=function(){
     if (xmlhttp.readyState==4 && xmlhttp.status==200)
      document.getElementById("passwordStatus").innerHTML=xmlhttp.responseText;
    }
  }

mysql和資料庫部分跟上篇博文的一樣沒有改變,運行結果如下圖

以上就是本文的全部內容,希望對大家的學習有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn