zoukankan      html  css  js  c++  java
  • ajax 检测用户名

     功能说明:当用户在注册页面输入用户名并且鼠标焦点离开输入框时,到数据表中去验证该用户名是否已经存在,如果存在提示【不可用】,否则,提示【可用】。

        必备知识:HTML、JavaScript、C#、ADO.NET,另外就是如何创建AJAX请求对象、使用open()和send()方法以及readyState、status、onreadystatechange、responseText的含义和用法。

        本例共用到2个页面:UserReg.aspx—用户注册页面(其实完全可以用静态页面)和 CheckUserName.aspx—把输入的用户名提交到这里验证。

        UserReg.aspx前台代码:

    Code
     1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UserReg.aspx.cs" Inherits="UserReg" %>
     2
     3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     4<html xmlns="http://www.w3.org/1999/xhtml">
     5<head runat="server">
     6    <title>无标题页</title>
     7
     8    <script language="javascript" type="text/javascript">
     9    <!--
    10    function CheckUserName(userName){
    11        var xmlhttp;
    12        try{
    13            xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
    14        }
    15        catch(e){
    16            try{
    17                xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
    18            }
    19            catch(e){
    20                try{
    21                    xmlhttp = new XMLHttpRequest();
    22                }
    23                catch(e)
    24                {}
    25            }
    26        }
    27       
    28        xmlhttp.onreadystatechange=function(){
    29            if(xmlhttp.readyState == 4){
    30                if(xmlhttp.status == 200){
    31                    var showMes = document.getElementById("showMes");
    32               
    33                    if(xmlhttp.responseText == "1"){
    34                        showMes.innerHTML="不可用";
    35                    }
    36                    else{
    37                        showMes.innerHTML= "可用";
    38                    }
    39                }
    40            }
    41           
    42        }
    43        xmlhttp.open("get","CheckUserName.aspx?userName="+escape(userName));
    44        xmlhttp.send(null);
    45    }
    46   
    47    //-->
    48    </script>
    49
    50</head>
    51<body>
    52    <form id="form1" runat="server">
    53    <div>
    54        用户名<asp:TextBox ID="txtUserName" runat="server" onblur="CheckUserName(this.value);"></asp:TextBox>
    55        <span id="showMes"></span>
    56        <br />
    57        密&nbsp;&nbsp;&nbsp;&nbsp;码<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
    58    </div>
    59    </form>
    60</body>
    61</html>
    62
        UserReg.aspx后台代码:无

        

         CheckUserName.aspx前台代码(把其他的HTML代码都清除,只留这一行):

        

    Code
    1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CheckUserName.aspx.cs" Inherits="CheckUserName" %>
    2
         CheckUserName.aspx后台代码:

       

    Code
     1using System;
     2using System.Collections;
     3using System.Configuration;
     4using System.Data;
     5using System.Linq;
     6using System.Web;
     7using System.Web.Security;
     8using System.Web.UI;
     9using System.Web.UI.HtmlControls;
    10using System.Web.UI.WebControls;
    11using System.Web.UI.WebControls.WebParts;
    12using System.Xml.Linq;
    13using System.Data.SqlClient;
    14
    15public partial class CheckUserName : System.Web.UI.Page
    16{
    17    protected void Page_Load(object sender, EventArgs e)
    18    {
    19        using (SqlConnection conn = new SqlConnection(
    20            System.Configuration.ConfigurationManager.AppSettings["ConnStr"]))
    21        {
    22            SqlCommand cmd = new SqlCommand();
    23            cmd.Connection = conn;
    24            cmd.CommandText = string.Format(
    25                "Select Count(*) From PUsers Where UserName='{0}'",
    26                Request.QueryString["userName"]);
    27            conn.Open();
    28            if (int.Parse(cmd.ExecuteScalar().ToString()) == 1)
    29            {
    30                Response.Write("1");
    31            }
    32            else
    33            {
    34                Response.Write("2");
    35            }
    36        }
    37    }
    38}

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/shuilv2000/archive/2009/02/17/3898801.aspx

  • 相关阅读:
    异步底层代码实现邮件发送
    MongoDB+Echarts+DWebSocket
    celery定时任务+redis有序集合实现实时访问人数
    位运算+数据库两种方式实现中间件权限操作
    cocoapod 引入url
    pdf转xml
    Flutter项目安卓下载地址
    ios Mac 利用SVN进行cocoapod私有库的使用
    KVO
    类别和类扩展的区别
  • 原文地址:https://www.cnblogs.com/tangself/p/1624144.html
Copyright © 2011-2022 走看看