利用Ajax检测用户名是否被占用的完整实例

适合人群:ajax和jquery入门

采用ajax实现用户名验证

使用jquery给出提示信息

用户注册的时候,使用ajax实现检测用户名是否已经被注册过,很多细节没有实现,给大家做个简单普及。

<%@ 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 = "该手机号已经被注册过";
						$("#msg").text("该手机号已经被注册");
						$("#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">请输入手机号<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"><br>
<input type="button" id="sub" value="提交" onclick="submit();">
</form>
</body>
</html>

下面是validateservlet模拟实现,没有做真正的数据库表数据检测,大家自行完成。

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检测用户名是否被占用的文章就介绍到这了,更多相关ajax检测用户名被占用内容请搜索硕编程以前的文章或继续浏览下面的相关文章希望大家以后多多支持硕编程!

相关文章