zoukankan      html  css  js  c++  java
  • AJAX+JavaScript无刷新检查用户名

    AJAX+JavaScript无刷新检查用户名是否可用2009-04-20 16:26   JavaScript 和 Ajax 代码
    <script language="javascript" type="text/javascript">
    var xmlHttp = null;
            function createXMLHttp()
            {
                if(window.ActiveXObject)
                    {
                        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                else if(window.XMLHttpRequest)
                    {
                        xmlHttp = new XMLHttpRequest();
                    }
            }
            function checka()
            {
                createXMLHttp();              
                var txtName=document.getElementById("txt_name"); // 获取输入用户名控件
                var labName=document.getElementById("lab_name"); // 显示提示信息的控件
       // 判断用户名格式和长度(只能是数字和字母组合)
       // 错误情况一
                    if(!txtName.value.match(/^[a-zA-Z0-9]+$/) && txtName.value.length < 5 || txtName.value.length >16)
                        {  
                          labName.style.color="#cc0000";
                          labName.value="请输入6-12位用户名,只能由数字字母组合。";
                          return false;
                            }else
        // 正确
                          if(txtName.value.length > 5 && txtName.value.length <17 && txtName.value.match(/^[a-zA-Z0-9]+$/))
                          {               
                                labName.style.color="#0000ff";
                                var url = "CheckName.aspx?userName="+txtName.value;
                                xmlHttp.open("post",url,true);
                                xmlHttp.onreadystatechange = sub;
                                xmlHttp.send(null);
                          }
        // 错误情况二
        else
                          {
                            labName.style.color="#cc0000";
                            labName.value="请输入6-12位用户名,只能由数字字母组合。";   
                            return false;       
                          }
                   
            }
            function sub()
            {
                if(xmlHttp.readyState==4)
                {
                    if(xmlHttp.status==200)
                    {
                        document.getElementById("lab_name").value=xmlHttp.responseText;
                    }
                }
            }

    </script>

    HTML 页面控件以及方法调用
    // onfocus 获取到光标事件 onblur 光标离开时候事件
    <asp:TextBox ID="txt_name" runat="server" CssClass="t_txt" onfocus="checkName();" onblur="checka();"></asp:TextBox>
    // 我用的 TextBox ,兼容 IE 和 Firefox 样式
    <asp:TextBox ID="lab_name" runat="server" Width="300px" style="border-top-style:none; border-0px; border-left-style:none; border-bottom-style:none; border-right-style:none;" ReadOnly="true"></asp:TextBox>

    新建一张页面具体数据操作

    SQL 连接以及语句略……

    新页面 方法
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    if (CheckNames(Request.QueryString["userName"]) == true)
                    {
                        Response.Write("对不起,该用户名已被占用!");
                    }
                    else
                    {
                        Response.Write("恭喜,该用户名可以使用!");
                    }
                }

            }

    private bool CheckNames(string userName)
            {
                // 创建一个数据集
                DataSet ds = new DataSet();
                // 实例化模型层
                ZYB_UserLogin uiModel = new ZYB_UserLogin();
                // 封装数据
                uiModel.UNL_UserLoginName = userName;
                // 实例化业务层
                UserLoginBLL uiBll = new UserLoginBLL();
                try
                {
                    // 调用业务层的检查用户名方法
                    ds = uiBll.SelectNameCheck(uiModel);
                    // 判断是否有数据
                    if (ds.Tables[0].Rows.Count < 1)
                    {
                        // 没有代表可用                                      
                        return false;
                    }
                    else
                    {
                        // 不可用                   
                        return true;
                    }
                }
                catch (Exception ex)
                {
                    // 抛出异常
                    throw ex;
                }
            }

     

  • 相关阅读:
    POJ 3320 Jessica's Reading Problem
    引用参数和传值参数的区别
    IBM 的数据库Informix 常用代语法
    设计模式之原型模式
    UBoot中使用tftp下载文件出现错误TFTP error: 'Access violation' (2)的解决办法
    printf格式控制符的完整格式
    如何打印hostent结构体中的所有数据
    Informix 数据库客户端 dbvisualizer SQL Commander 乱码解决方案
    设计模式之模版方法模试
    nfs: server 192.168.37.200 not responding, still trying的解决办法
  • 原文地址:https://www.cnblogs.com/liufei88866/p/1599883.html
Copyright © 2011-2022 走看看