Ajax實現非同步重新整理驗證使用者名稱是否已存在的具體方法

來源:互聯網
上載者:User

 由於要做一個註冊頁面,看到許多網站上都是使用Ajax非同步重新整理驗證使用者名稱是否可用的,所以自己也動手做一個小執行個體

都是簡單的執行個體,所以直接發代碼 靜態頁面Ajax.html 代碼如下:<html>    <head>        <title>Ajax</title>        <script type="text/javascript">            function loadXMLDoc() {                if (document.getElementById("account").value == "") {                    document.getElementById("accDiv").innerHTML = "使用者名稱不可為空";                    return;                }                var xmlHttp;                if(window.XMLHttpRequest) { // code for IE7+                    xmlHttp = new XMLHttpRequest();                }                else { // code for IE5/IE6                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");                }                 xmlHttp.onreadystatechange = function () {                    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {                        //document.getElementById("myDiv").innerHTML=xmlHttp.responseText;                        if (xmlHttp.responseText == "true") {                            document.getElementById("accDiv").innerHTML = "使用者名稱不可用";                        }                        else {                            document.getElementById("accDiv").innerHTML = "使用者名稱可用";                        }                    }                }                var a = document.getElementById("account").value;                // get                xmlHttp.open("GET", "validate.aspx?account=" + a + "&random=" + Math.random, true);                xmlHttp.send();            }            function delData() {                document.getElementById("account").value = "";                document.getElementById("accDiv").innerHTML = "";            }        </script>    </head>    <body>        <h3>ajax</h3>        <table>            <tr>                <td>帳號:</td><td><input id="account" type="text" onblur="loadXMLDoc();" onfocus="delData();"/></td><td><div id="accDiv"></div></td>            </tr>            <tr>                <td>密碼:</td><td><input id="passwd" type="password" /></td>            </tr>            <tr>                <td>確認密碼:</td><td><input id="vPasswd" type="password" /></td>            </tr>            <tr>                <td>姓名:</td><td><input id="name" type="text" /></td>            </tr>        </table>     </body></html>  在帳號輸入框失去焦點時調用函數 訪問伺服器使用的是Get方法,所以在參數處使用了附加隨機碼來避免緩衝。 驗證頁面validate.aspx後台代碼:代碼如下:using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Configuration;using System.Data.Sql;using System.Data.SqlClient; public partial class Ajax_validate_validate : System.Web.UI.Page{    public SqlConnection conn;     protected void Page_Load(object sender, EventArgs e)    {        Response.Clear();        if (Exists(Request.QueryString["account"]))            Response.Write("true");        else            Response.Write("false");        Response.End();    }    /// <summary>    /// 擷取資料庫連接    /// </summary>    /// <returns></returns>    protected SqlConnection GetConnection()    {        string str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;        conn = new SqlConnection(str);        return conn;    }    protected bool Exists(string account)    {        using (GetConnection())        {            try            {                conn.Open();                string sqlStr = "select count(*) from userinfo where account='" + account + "'";                SqlCommand cmd = new SqlCommand(sqlStr, conn);                int row = Convert.ToInt32(cmd.ExecuteScalar());                if (row > 0)                    return true;                else                    return false;            }            catch (Exception e)            {                throw e;            }            finally            {                conn.Close();            }        }    }}   在後台中驗證使用者名稱是否已經存在於資料庫中,返回真或者假 資料庫很簡單,只建了一張表userinfo,有3個欄位:account、passwd、name 注意:在後台往請求頁面寫資料時,當寫完要發送的資料之後,需要調用Response.end()方法來終止寫入,否則可能會發送一個完整頁面過去。 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.