Ajax實現(xiàn)異步刷新驗證用戶名是否已存在的具體方法__第1頁
Ajax實現(xiàn)異步刷新驗證用戶名是否已存在的具體方法__第2頁
Ajax實現(xiàn)異步刷新驗證用戶名是否已存在的具體方法__第3頁
Ajax實現(xiàn)異步刷新驗證用戶名是否已存在的具體方法__第4頁
Ajax實現(xiàn)異步刷新驗證用戶名是否已存在的具體方法__第5頁
免費預覽已結束,剩余3頁可下載查看

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、Ajax實現(xiàn)異步刷新驗證用戶名是否已存在的具體方法_ 由于要做一個注冊頁面,看到很多網站上都是用法Ajax異步刷新驗證用戶名是否可用的,所以自己也動手做一個小實例 都是簡潔的實例,所以挺直發(fā)代碼 靜態(tài)頁面Ajax.html 代碼如下: html head titleAjax/title script type=text/javascript function loadXMLDoc() if (document.getElementById(account).value = ) document.getElementById(accDiv).innerHTML = 用戶名不能為空; return

2、; 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

3、.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(

4、); function delData() document.getElementById(account).value = ; document.getElementById(accDiv).innerHTML = ; /script /head body h3ajax/h3 table tr td賬號:/tdtdinput id=account type=text onblur=loadXMLDoc(); onfocus=delData();/tdtddiv id=accDiv/div/td /tr tr td密碼:/tdtdinput id=passwd type=password /t

5、d /tr tr td確認密碼:/tdtdinput id=vPasswd type=password /td /tr tr td姓名:/tdtdinput 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 Syst

6、em.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(Re

7、quest.QueryStringaccount) Response.Write(true); else Response.Write(false); Response.End(); / summary / 獵取數據庫連接 / /summary / returns/returns protected SqlConnection GetConnection() string str = ConfigurationManager.ConnectionStringsConnectionString.ConnectionString; conn = new SqlConnection(str); re

8、turn 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(); 在后臺中驗證用戶名是否已經存在于數據庫中,返回真或者假 數據庫很

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論