首页 > 技术文章 > ajax检查用户名是否存在

liupeng61624 2014-04-14 11:02 原文

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CheckUser.Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>检查用户名是否存在</title>
    <script type="text/javascript">
        var xmlHttp = null;

        function createXMLHttpRequest() {
            if (xmlHttp == null) {
                if (window.XMLHttpRequest) {
                    //Mozilla 浏览器
                    xmlHttp = new XMLHttpRequest();
                } else if (window.ActiveXObject) {
                    // IE浏览器
                    try {
                        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
                    } catch (e) {
                        try {
                            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e) {
                            //alert('创建失败');
                        }
                    }
                }
            }
        }
        function openAjax() {
            if (xmlHttp == null) {
                createXMLHttpRequest();
                if (xmlHttp == null) {
                    //alert('出错');
                    return;
                }
            }

            var val = document.getElementById('txt').value;

            xmlHttp.open("get", "VerifyUserNameHandler.ashx?para=" + val + "&date=" + new Date(), true);
            xmlHttp.onreadystatechange = xmlHttpChange;
            xmlHttp.send(null);

            document.getElementById('resultSpan').innerText = '正在检查,请稍候...';
        }

        function xmlHttpChange() {
            if (xmlHttp.readyState == 4) {
                if (xmlHttp.status == 200) {
                    var res = xmlHttp.responseText;
                    document.getElementById('resultSpan').innerText = res;

                    if (res == '恭喜,用户名可以使用。') {
                        setTimeout("document.getElementById('resultSpan').innerText='';", 2000);
                    }
                    else if (res == '抱歉,用户名已被使用。') {
                        document.getElementById('txt').focus();
                    }
                }
            }
        }       
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        用户名:<input type="text" id='txt' value="" onblur="openAjax();" />
        <span id="resultSpan"></span>
    </div>
    </form>
</body>
</html>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CheckUser
{
    /// <summary>
    /// VerifyUserNameHandler 的摘要说明
    /// </summary>
    public class VerifyUserNameHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
            string _name = context.Request.QueryString["para"];
            _name = string.IsNullOrEmpty(_name) ? "" : _name;
            System.Threading.Thread.Sleep(500);//用线程来模拟数据库查询工作
            string[] Names = new string[] { "pannian", "liupeng", "chenhao" };//这里用Names数组来代替数据库中的结果集
            if (Array.IndexOf<string>(Names, _name) == -1)
            {
                context.Response.Write("恭喜,用户名可以使用。");
            }
            else
            {
                context.Response.Write("抱歉,用户名已被使用。");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

推荐阅读