溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何利用Ajax檢測用戶名是否被占用

發(fā)布時間:2021-03-16 15:23:50 來源:億速云 閱讀:126 作者:小新 欄目:web開發(fā)

小編給大家分享一下如何利用Ajax檢測用戶名是否被占用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

采用Ajax實現(xiàn)用戶名驗證

使用jQuery給出提示信息

用戶注冊的時候,使用Ajax實現(xiàn)檢測用戶名是否已經(jīng)被注冊過,很多細(xì)節(jié)沒有實現(xiàn),給大家做個簡單普及。

<%@ page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>用戶注冊頁面</title>
<script src="https://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
	var xmlHttp;
	function createXMLHttpRequest(){
		if(window.XMLHttpRequest){
			xmlHttp = new XMLHttpRequest();
		}else if(window.ActiveXObject){
			xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
		}
	}
	function validate(account){
		createXMLHttpRequest();
		xmlHttp.open("Get","ValidateServlet?account="+account,true);
		xmlHttp.onreadystatechange = callback;
		xmlHttp.send(null);
	}
	function callback(){
		if(xmlHttp.readyState==4){
				if(xmlHttp.status==200){
					var text = xmlHttp.responseText;
					if(text=="true"){
						//document.getElementById("msg").innerHTML = "該手機(jī)號已經(jīng)被注冊過";
						$("#msg").text("該手機(jī)號已經(jīng)被注冊");
						$("#sub").attr("disabled","true");//添加disabled屬性,讓按鈕不可用
					}else{
						//document.getElementById("msg").innerHTML = "";
						$("#msg").text("");
						$("#sub").removeAttr("disabled");//移除disabled屬性,讓按鈕可用
						
					}
				}else{
					alert("請求失敗,錯誤碼="+xmlHttp.status);
				}
		}
	}
	function checkInfo(){
		var account = $("#account").val();
		var pwd1 = $("#pwd1").val();
		var pwd2 = $("#pwd2").val();
		if(account==""||account==null){
			$("#msg").text("賬號不能為空");
			$("#sub").attr("disabled","true");
			return false;
		}
		if(pwd1==""||pwd1==null||pwd2==""||pwd2==null||pwd1!=pwd2){
			$("#info").text("密碼不能為空或者兩次密碼不一致");
			$("#sub").attr("disabled","true");
			return false;
		}
		$("#msg").text("");
		$("#info").text("");
		$("#sub").removeAttr("disabled");
	}
	function submit(){
		checkInfo();
		$("#reg").submit();
	}
 
</script>
</head>
<body>
<form id="reg" name="reg" action="RegisterServlet" method="post">
賬號:<input type="text" name="account" id="account" onblur="validate(this.value);">
<span id="msg" >請輸入手機(jī)號</span><br>
密碼:<input type="password" id="pwd1" name="password1" onblur="checkInfo();"><br>
確認(rèn)密碼:<input type="password" id="pwd2" name="password2" onblur="checkInfo();">
<span id="info" ></span><br>
<input type="button" id="sub" value="提交" onclick="submit();">
</form>
</body>
</html>

下面是ValidateServlet模擬實現(xiàn),沒有做真正的數(shù)據(jù)庫表數(shù)據(jù)檢測,大家自行完成。

package com.ambow.servlet;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
@WebServlet("/ValidateServlet")
public class ValidateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
 public ValidateServlet() {
  super(); 
 }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter pw = response.getWriter();
		String account = request.getParameter("account");
		System.out.println("account"+account);
		if("123".equals(account)) {
			pw.print("true");
		}else {
			pw.print("false");
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

以上是“如何利用Ajax檢測用戶名是否被占用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI