濮阳杆衣贸易有限公司

主頁 > 知識庫 > 利用Ajax檢測用戶名是否被占用的完整實例

利用Ajax檢測用戶名是否被占用的完整實例

熱門標簽:地圖標注與公司業(yè)務(wù)關(guān)系 提高電話機器人接通率 銷售電銷機器人詐騙 大學(xué)校門地圖標注 福建微碼電話機器人 荊州智能電銷機器人 平?jīng)龈叩碌貓D標注商戶要收費嗎 外呼系統(tǒng)api對接 廣西智能外呼系統(tǒng)多少錢

適合人群:Ajax和jQuery入門

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

使用jQuery給出提示信息

用戶注冊的時候,使用Ajax實現(xiàn)檢測用戶名是否已經(jīng)被注冊過,很多細節(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īng)被注冊過";
						$("#msg").text("該手機號已經(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" style="color:red">請輸入手機號/span>br>
密碼:input type="password" id="pwd1" name="password1" onblur="checkInfo();">br>
確認密碼:input type="password" id="pwd2" name="password2" onblur="checkInfo();">
span id="info" style="color:red">/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);
	}
}

到此這篇關(guān)于利用Ajax檢測用戶名是否被占用的文章就介紹到這了,更多相關(guān)Ajax檢測用戶名被占用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • JQuery Ajax如何實現(xiàn)注冊檢測用戶名
  • PHP+Ajax實現(xiàn)的檢測用戶名功能簡單示例
  • AJAX應(yīng)用實例之檢測用戶名是否唯一(實例代碼)
  • AJAX實現(xiàn)無刷新檢測用戶名功能
  • jQuery+Ajax實現(xiàn)用戶名重名實時檢測
  • 使用Ajax實時檢測"用戶名、郵箱等"是否已經(jīng)存在
  • PHP+Ajax檢測用戶名或郵件注冊時是否已經(jīng)存在實例教程
  • Asp.net下利用Jquery Ajax實現(xiàn)用戶注冊檢測(驗證用戶名是否存)
  • jquery ajax 檢測用戶注冊時用戶名是否存在
  • PHP+AJAX實現(xiàn)無刷新注冊(帶用戶名實時檢測)
  • PHP+AJAX實現(xiàn)無刷新注冊(帶用戶名實時檢測)
  • ajax 檢測用戶名是否被占用
  • AJAX檢測用戶名是否存在的方法

標簽:黔東 邯鄲 婁底 樂山 衡陽 德陽 內(nèi)江 海南

巨人網(wǎng)絡(luò)通訊聲明:本文標題《利用Ajax檢測用戶名是否被占用的完整實例》,本文關(guān)鍵詞  利用,Ajax,檢測,用,戶名,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《利用Ajax檢測用戶名是否被占用的完整實例》相關(guān)的同類信息!
  • 本頁收集關(guān)于利用Ajax檢測用戶名是否被占用的完整實例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    长兴县| 黑山县| 澳门| 新沂市| 通许县| 巴彦县| 江华| 新竹县| 常德市| 缙云县| 花莲市| 濮阳市| 独山县| 黑河市| 北宁市| 建宁县| 开封市| 仁化县| 兴城市| 霍林郭勒市| 怀化市| 克拉玛依市| 阿拉善盟| 都江堰市| 轮台县| 伊春市| 松阳县| 桓仁| 峨山| 三江| 遂昌县| 永嘉县| 河池市| 尤溪县| 北碚区| 郓城县| 云阳县| 庄河市| 寿阳县| 德保县| 泊头市|