我原本也打算用ajax的,但当时可能没找到什么好的组件,自己也不是很熟悉,所以就放弃了。其实我这个感觉也有点像ajax哈哈,可能是最简单的ajax吧。
下面的代码很简单,一般都能看懂,实在不清楚的也不要不好意思,你可以在评论里讨论,也可以给我发邮件。
需要验证的textbox里加上属性οnblur=CheckRegUser(你要判断的字符);
function CheckRegUser(str)
{
var ele = document.getElementById("divTips");
var btn = document.getElementById("RegUserInfo1_btnReg");
if(GetData("GetData.aspx","CHECKUSERREGED","Username=" + str,true)=='1')
//GetData()这个函数调用下面的函数,返回你自己定义的字符串,GetData.aspx这个网页根据你传递的参数返回你需要实现的功能的字符串,注意只输出你需要的字符串,需要用到response.clear response.end 中间用response.write输出你要返回的字符串,比如0 或者 1
{
ele.innerHTML = '<font color=red><b>相同的用户名已经存在</b></font>';
btn.disabled = true;
}
else
{
ele.innerHTML = '<font color=black>恭喜你,该用户名可以被你拥有</font>';
btn.disabled = false;
}
}
GetData.aspx页面中的部分示例代码:
Protected strFunc As String = String.Empty
Protected strTemp As String = String.Empty
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
strFunc = Replace(Request.QueryString("FuncCode") & "", "'", "")
Catch ex As Exception
SendErr(ex.Message.ToString & "_" & ex.ToString & "_" & ex.StackTrace)
strFunc = ""
End Try
Response.Clear()
Select Case strFunc.ToUpper.ToString
Case "CHECKUSERREGED"
Dim strUsername As String = Request.QueryString("UserName").ToString
strusername = strusername.Replace("'", "''")
Dim strFilter As String = "|CHAT|BBS|WWW|MAIL|KANDY|DOWNLAOD|DOWN|FUCK|KANDYCHEN|KANDYASP|SEX|FUCKYOU|"
If strUsername.Trim.ToString <> "" Then
If InStr(strFilter.ToUpper.ToString, "|" & Trim(strUsername).ToUpper.ToString & "|") > 0 Then
strTemp = "1"
Else
GetDataSet("select * from userinfo where UserName='" & strUsername & "'", "UserRigisterUsername", da)
If Not IsNothing(dsReturn.Tables("UserRigisterUsername")) AndAlso dsReturn.Tables("UserRigisterUsername").Rows.Count > 0 Then
strTemp = "1"
Else
strTemp = "0"
End If
dsReturn.Tables("UserRigisterUsername").Clear()
End If
Else
strTemp = "1"
End If
Response.Write(strTemp)
End Select
Response.End()
End Sub
//Writer : Kandy Chen
//QQ : 1638988
//Email : 1638988@gmail.com
//Blog : http://kandy.cnblogs.com/
//=========================================================
//=Url : "GetData.aspx" =
//=Func : "CheckUser" =
//=Params : "Username=kandy&Password=123456" =
//=bFresh : "true" =
//=========================================================
function GetData(Url,Func,Params,bFresh)
{
var dt = new Date(); //get date string
var sTime;
if(bFresh) //if need fresh data
{
//update new time string
sTime=dt.getFullYear().toString() + dt.getMonth().toString() + dt.getDate().toString() + dt.getHours().toString() + dt.getMinutes().toString() + dt.getSeconds().toString() + dt.getTime().toString();
}
else
{
sTime="KandyAsp_@__Hotmail_Com_And_My_QQ_is_1638988" + dt.getFullYear().toString() + dt.getMonth().toString() + dt.getDate().toString() + dt.getHours().toString() + parseInt(dt.getMinutes()/10);
}
var strUrl = Url + "?FuncCode=" + Func + "&" + Params + "&sTime=" + sTime;
var objHttp = new ActiveXObject("Microsoft.XMLHTTP");
objHttp.Open("Get",strUrl,false);
objHttp.Send(); //send to the server
if(objHttp.status == 200) //connect ok
{
return objHttp.responseText; //return need data
}
else //can not connect to the server
{
return "Something is wrong with the net,can not connect to the server,please try later!";
}
}