javascript和jquery實現(xiàn)用戶登錄驗證
更新時間:2022年03月25日 10:39:02 作者:尼阿卡
這篇文章主要為大家詳細介紹了javascript和jquery分別實現(xiàn)用戶登錄驗證的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
在上一篇文章Ajax實現(xiàn)異步用戶名驗證功能中,用javascript實現(xiàn)了用戶驗證,但并沒有對密碼進行驗證,這次追加了這個功能,并分別用javascript和jquery實現(xiàn)。
一.用jquery的ajax實現(xiàn)的關鍵代碼
實現(xiàn)如下
/*jquery實現(xiàn)
$(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("賬號發(fā)生錯誤!")
},
});
});
$("#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("密碼查詢發(fā)生錯誤!")
},
});
});
}); */二.用javascript實現(xiàn)的關鍵代碼
實現(xiàn)如下
//javascript實現(xiàn)
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和數(shù)據(jù)庫部分跟上篇博文的一樣沒有改變,運行結果如下圖

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助。
相關文章
通過循環(huán)優(yōu)化 JavaScript 程序
這篇文章主要介紹了通過循環(huán)優(yōu)化 JavaScript 程序,對于提高 JavaScript 程序的性能這個問題,最簡單同時也是很容易被忽視的方法就是學習如何正確編寫高性能循環(huán)語句。下面我們來學習一下吧2019-06-06
基于JavaScript實現(xiàn)前端文件的斷點續(xù)傳
這篇文章主要介紹了基于JavaScript實現(xiàn)前端文件的斷點續(xù)傳的相關資料,需要的朋友可以參考下2016-10-10
JavaScript中的this指向綁定規(guī)則及常見面試總結
這篇文章主要為大家介紹了JavaScript中的this指向綁定規(guī)則及箭頭韓碩中的this指向,還b包含了常見面試總結,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12

