zoukankan      html  css  js  c++  java
  • ajax检查用户名是否存在

    <%@ 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;
                }
            }
        }
    }

  • 相关阅读:
    趣谈编程史第4期-饱受争议的前端之王JavaScript的血泪成长史
    趣谈编程史第2期-这个世界缺少对C语言的敬畏,你不了解的C语言科普
    趣谈编程史第1期-跌宕起伏的java帝国史,剖析谷歌甲骨文长达8年的版权战争
    记录一次Metaspace扩容引发FGC的调优总结
    多线程学习笔记-深入理解ThreadPoolExecutor
    使用CompletableFuture优化你的代码执行效率
    Linux+Shell常用命令总结
    Guava Cache探索及spring项目整合GuavaCache实例
    将List按照指定大小等分的几种实现方式和效率对比及优化
    Spring的事件机制详解
  • 原文地址:https://www.cnblogs.com/liupeng61624/p/3663571.html
Copyright © 2011-2022 走看看