利用Ajax檢測(cè)用戶名是否被占用的完整實(shí)例
適合人群:Ajax和jQuery入門
采用Ajax實(shí)現(xiàn)用戶名驗(yàn)證
使用jQuery給出提示信息
用戶注冊(cè)的時(shí)候,使用Ajax實(shí)現(xiàn)檢測(cè)用戶名是否已經(jīng)被注冊(cè)過(guò),很多細(xì)節(jié)沒有實(shí)現(xiàn),給大家做個(gè)簡(jiǎ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>用戶注冊(cè)頁(yè)面</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ī)號(hào)已經(jīng)被注冊(cè)過(guò)";
$("#msg").text("該手機(jī)號(hào)已經(jīng)被注冊(cè)");
$("#sub").attr("disabled","true");//添加disabled屬性,讓按鈕不可用
}else{
//document.getElementById("msg").innerHTML = "";
$("#msg").text("");
$("#sub").removeAttr("disabled");//移除disabled屬性,讓按鈕可用
}
}else{
alert("請(qǐng)求失敗,錯(cuò)誤碼="+xmlHttp.status);
}
}
}
function checkInfo(){
var account = $("#account").val();
var pwd1 = $("#pwd1").val();
var pwd2 = $("#pwd2").val();
if(account==""||account==null){
$("#msg").text("賬號(hào)不能為空");
$("#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">
賬號(hào):<input type="text" name="account" id="account" onblur="validate(this.value);">
<span id="msg" style="color:red">請(qǐng)輸入手機(jī)號(hào)</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" style="color:red"></span><br>
<input type="button" id="sub" value="提交" onclick="submit();">
</form>
</body>
</html>
下面是ValidateServlet模擬實(shí)現(xiàn),沒有做真正的數(shù)據(jù)庫(kù)表數(shù)據(jù)檢測(cè),大家自行完成。
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檢測(cè)用戶名是否被占用的文章就介紹到這了,更多相關(guān)Ajax檢測(cè)用戶名被占用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- JQuery Ajax如何實(shí)現(xiàn)注冊(cè)檢測(cè)用戶名
- PHP+Ajax實(shí)現(xiàn)的檢測(cè)用戶名功能簡(jiǎn)單示例
- AJAX應(yīng)用實(shí)例之檢測(cè)用戶名是否唯一(實(shí)例代碼)
- AJAX實(shí)現(xiàn)無(wú)刷新檢測(cè)用戶名功能
- jQuery+Ajax實(shí)現(xiàn)用戶名重名實(shí)時(shí)檢測(cè)
- 使用Ajax實(shí)時(shí)檢測(cè)"用戶名、郵箱等"是否已經(jīng)存在
- PHP+Ajax檢測(cè)用戶名或郵件注冊(cè)時(shí)是否已經(jīng)存在實(shí)例教程
- Asp.net下利用Jquery Ajax實(shí)現(xiàn)用戶注冊(cè)檢測(cè)(驗(yàn)證用戶名是否存)
- jquery ajax 檢測(cè)用戶注冊(cè)時(shí)用戶名是否存在
- PHP+AJAX實(shí)現(xiàn)無(wú)刷新注冊(cè)(帶用戶名實(shí)時(shí)檢測(cè))
- PHP+AJAX實(shí)現(xiàn)無(wú)刷新注冊(cè)(帶用戶名實(shí)時(shí)檢測(cè))
- ajax 檢測(cè)用戶名是否被占用
- AJAX檢測(cè)用戶名是否存在的方法
相關(guān)文章
AJAX實(shí)現(xiàn)文件上傳功能報(bào)錯(cuò)Current request is not a&n
這篇文章主要介紹了AJAX文件上傳功能實(shí)現(xiàn)中出現(xiàn)報(bào)錯(cuò)Current request is not a multipart request的問題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
基于CORS實(shí)現(xiàn)WebApi Ajax 跨域請(qǐng)求解決方法
這篇文章主要介紹了基于CORS實(shí)現(xiàn)WebApi Ajax 跨域請(qǐng)求解決方法,需要的朋友可以參考下2017-08-08
ajax三級(jí)聯(lián)動(dòng)的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了ajax三級(jí)聯(lián)動(dòng)的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
ajax局部刷新一個(gè)div下jsp內(nèi)容的方法
局部刷新某個(gè)div下的jsp可以通過(guò)setInterval或者是setTimeout來(lái)輕松實(shí)現(xiàn),具體如下,有此需求的朋友可以參考下,希望對(duì)大家有所幫助2013-08-08
Ajax?請(qǐng)求隊(duì)列解決方案并結(jié)合elementUi做全局加載狀態(tài)
這篇文章主要介紹了Ajax?請(qǐng)求隊(duì)列解決方案并結(jié)合elementUi做全局加載狀態(tài),使用 消息隊(duì)列 制作 請(qǐng)求防抖,防止重復(fù)請(qǐng)求印象服務(wù)器,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10
Ajax請(qǐng)求發(fā)送成功但不進(jìn)success的解決方法
下面小編就為大家分享一篇Ajax請(qǐng)求發(fā)送成功但不進(jìn)success的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
如何利用jQuery post傳遞含特殊字符的數(shù)據(jù)
在jquery中,解決數(shù)據(jù)傳遞處理的方法我們通常利用$.ajax或$.post,但是這里這里通常不能傳遞特殊字符,比如說(shuō):“<”,本文就幫大家解決如何傳遞這種含特殊字符的數(shù)據(jù),感興趣的朋友一起看下吧2015-10-10
AJax實(shí)現(xiàn)類似百度搜索欄的功能 (面試多見)
下面是ajax實(shí)現(xiàn)一個(gè)簡(jiǎn)單的百度搜索欄的功能,當(dāng)用戶在上面的輸入框中鍵入字符時(shí),會(huì)執(zhí)行函數(shù) "showHint()" 。下文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2016-11-11

