本文主要是向大家介绍如何通过Ajax检测用户名占用与否的相关资料,通过示例代码介绍非常详细,对大家的学习或工作都有一定的参考和学习价值,需要学习的朋友们下面和小编一起学习吧

适用人群: Ajax和 jQuery入门

使用 Ajax进行用户名验证

给 jQuery提供提示信息

当用户注册时,用 Ajax实现检测用户名是否已注册,很多细节没有实现,给大家做一个简单普及。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<%@ 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 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("www.51iyq.com");
        $("#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模拟实现,没有做真正的数据库表数据检测,大家自行完成。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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检测用户名被占用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!