ajax 檢測(cè)用戶名是否被占用
更新時(shí)間:2006年06月28日 00:00:00 作者:
有時(shí)候在注冊(cè)窗口,為了用戶體驗(yàn)我們會(huì)用ajax無刷新檢測(cè)用戶名是否已被注冊(cè)
復(fù)制代碼 代碼如下:
//檢測(cè)用戶名是否被占用
function GetO(){
var ajax=false;
try {
ajax = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajax = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
ajax = false;
}
}
if (!ajax && typeof XMLHttpRequest!='undefined') {
ajax = new XMLHttpRequest();
}
return ajax;
}
function getResult(serverPage) {
var ajax = GetO();
var sep = (-1 < serverPage.indexOf("?")) ? "&" : "?"
var newurl = serverPage + sep + "__=" + encodeURIComponent((new Date()).toString());
ajax.open("GET", newurl, true);
ajax.onreadystatechange = function() {
if (ajax.readyState == 4 && ajax.status == 200) {
if(ajax.responseText==1){
ShowResult(1,"您的用戶名可以正常注冊(cè)")
AjaxResult=true;
}else{
ShowResult(0,"您的用戶名被占用了")
AjaxResult=false;
}
}
}
ajax.send(null);
}
function chkUsername(){
var sflag;//標(biāo)記;
var sErr;//錯(cuò)誤信息;
sflag=true;
var re=/^\w{2,20}$/;
if(!re.test(document.Form1.UserName.value)){
sflag=false;
sErr="用戶名只能輸入字母、數(shù)字或下劃線";
}
if (document.Form1.UserName.value.length>20||document.Form1.UserName.value.length<2){
sflag=false;
sErr="用戶名長(zhǎng)度必須在2~20個(gè)字符之間";
}
if (document.Form1.UserName.value=="" ){
sErr="用戶名不能為空";
sflag=false;
}
//ajax檢測(cè)返回結(jié)果
if (!sflag) {
ShowResult(0,sErr)
}else{
getResult("../MemberCheck.aspx?t=1&Name=" + document.Form1.UserName.value);
}
//document.getElementById("spname").innerHTML=ss;
}
function ShowResult(tag,str){
var obj = document.getElementById("spname");
if(tag==0){
obj.style.border="1px solid red";
obj.style.color="red";
}else{
obj.style.border="1px solid green";
obj.style.color="green";
}
obj.innerHTML=str;
obj.style.padding="1px";
}
//取ajax返回的用戶問題值
function getQuestionData(sPage){
var ajax = GetO();
var sep = (-1 < sPage.indexOf("?")) ? "&" : "?"
var newurl = sPage + sep + "__=" + encodeURIComponent((new Date()).toString());
ajax.open("GET", newurl, true);
ajax.onreadystatechange = function() {
if (ajax.readyState == 4 && ajax.status == 200) {
if(ajax.responseText==""){
document.getElementById("getstate").innerHTML="用戶不存在!"
}
else{
document.g.Question.value=ajax.responseText;
document.g.Question.readOnly=true;
document.getElementById("getpwdstep1").style.display="none";
document.getElementById("getpwdstep2").style.display="block";
document.getElementById("getstate").innerHTML="";
}
}
}
ajax.send(null);
}
function getUserQ(){
//改變報(bào)錯(cuò)span的顯示樣式
document.getElementById("getstate").style.padding="1px 4px";
if (document.g.UserName.value==""){
document.getElementById("getstate").innerHTML="用戶名不能為空!"
}else{
document.getElementById("getstate").style.background="red";
document.getElementById("getstate").style.color="white";
document.getElementById("getstate").innerHTML="正在加載數(shù)據(jù)...";
//設(shè)置ajax
getQuestionData("../MemberCheck.aspx?t=2&gName=" + document.g.UserName.value);
}
}
function login(n,p,t)
{
if(t==false){
document.getElementById("pLogin").style.display="none";
document.getElementById("LoginInfo").innerHTML="正在幫您登錄,請(qǐng)稍等...";
}
var request;
if(undefined == request){
request = new Request();
}
if(undefined == request._get){
//不支持Ajax獲取數(shù)據(jù);
if(t==false){
document.getElementById("pLogin").style.display="";
document.getElementById("LoginInfo").innerHTML="不能獲取數(shù)據(jù)";
}
}
else{
userName=n;
toPrev=t;
request.GetNoCache(loginPath + "LoginCheck.aspx" + "?n=" + n + "&p=" + p,loginCheck);
}
}
function loginCheck(result)
{
if (result.readyState!=ReadyState.Complete)
return;
//成功獲取;
if (result.status==HttpStatus.OK && result.responseText != "")
{
//執(zhí)行返回的語句;
//alert(result.responseText);
if (result.responseText=='1')
{
if(toPrev ==false){
document.getElementById("LoginInfo").innerHTML='<ul><li>歡迎' + userName + '</li><li><a href="' + loginPath + 'member/">管理</a></li><li><a href="' + loginPath + 'Logout.aspx">退出</a></li></ul>';
}
else{
if(UrlPrev != ''){
document.location.href=UrlPrev;
}
else{
document.location.href=loginPath;
}
}
return;
}
else
{
if(document.location.href !="http://" + document.location.hostname + loginPath + "Member/Login.aspx")
document.location.href=loginPath + "Member/Login.aspx";
}
}
else
{
//失敗;
document.location.href=loginPath + "Member/Login.aspx";
return;
}
}
function checkKey(caputureKey,n,p,t){
var key = window.event.keyCode;
if(key == caputureKey){
login(n,p,t);
}
}
function setLoginInfo(bLogin){
if(bLogin){
document.getElementById("LoginInfo").innerHTML='<ul><li>歡迎' + userName + '</li><li><a href="' + loginPath + 'member/">管理</a></li><li><a href="' + loginPath + 'Logout.aspx">退出</a></li></ul>';
}
else{
}
}
function gotoUrl(s){
window.location.href=loginPath + 'Member/RegUser.html';
}
您可能感興趣的文章:
- 利用Ajax檢測(cè)用戶名是否被占用的完整實(shí)例
- JQuery Ajax如何實(shí)現(xiàn)注冊(cè)檢測(cè)用戶名
- PHP+Ajax實(shí)現(xiàn)的檢測(cè)用戶名功能簡(jiǎn)單示例
- AJAX應(yīng)用實(shí)例之檢測(cè)用戶名是否唯一(實(shí)例代碼)
- AJAX實(shí)現(xiàn)無刷新檢測(cè)用戶名功能
- jQuery+Ajax實(shí)現(xiàn)用戶名重名實(shí)時(shí)檢測(cè)
- 使用Ajax實(shí)時(shí)檢測(cè)"用戶名、郵箱等"是否已經(jīng)存在
- PHP+Ajax檢測(cè)用戶名或郵件注冊(cè)時(shí)是否已經(jīng)存在實(shí)例教程
- Asp.net下利用Jquery Ajax實(shí)現(xiàn)用戶注冊(cè)檢測(cè)(驗(yàn)證用戶名是否存)
- jquery ajax 檢測(cè)用戶注冊(cè)時(shí)用戶名是否存在
- PHP+AJAX實(shí)現(xiàn)無刷新注冊(cè)(帶用戶名實(shí)時(shí)檢測(cè))
- PHP+AJAX實(shí)現(xiàn)無刷新注冊(cè)(帶用戶名實(shí)時(shí)檢測(cè))
- AJAX檢測(cè)用戶名是否存在的方法
相關(guān)文章
使用ajax技術(shù)實(shí)現(xiàn)txt彈出在頁面上的方法
下面小編就為大家?guī)硪黄褂胊jax技術(shù)實(shí)現(xiàn)txt彈出在頁面上的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08基于Jquery.history解決ajax的前進(jìn)后退問題
本文主要給大家介紹基于Jquery.history解決ajax的前進(jìn)后退問題,涉及到j(luò)query前進(jìn)后退相關(guān)方面的知識(shí),本文內(nèi)容經(jīng)典,非常具有參考價(jià)值,特此把jquery前進(jìn)后退相關(guān)知識(shí)分享在腳本之家網(wǎng)站供大家參考2015-10-10js跨域調(diào)用WebService的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)硪黄猨s跨域調(diào)用WebService的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09Ajax使用異步對(duì)象發(fā)送請(qǐng)求方案詳解
Ajax的原理是通過XmlHttpRequest對(duì)象來向服務(wù)器發(fā)異步請(qǐng)求,從服務(wù)器獲得數(shù)據(jù),然后用JavaScript來操作DOM而更新頁面,這篇文章主要介紹了Ajax使用異步對(duì)象發(fā)送請(qǐng)求簡(jiǎn)介,需要的朋友可以參考下2024-04-04有關(guān)Ajax中g(shù)et和post的使用問題
下面小編就為大家?guī)硪黄嘘P(guān)Ajax中g(shù)et和post的使用問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06