在註冊的時候,常會進行使用者名稱驗證,如果輸入的使用者名稱已經存在,立刻給出提示而不是等資訊填寫完後進行驗證,如果輸入資訊很多的話,不僅資訊會丟失,並且會進行一次回傳,很惱火,那麼註冊的時候使用Ajax驗證一下,就可以避免這些問題了,下面給出最簡單的使用樣本,判斷使用者名稱是否存在:
轉自:http://www.cnblogs.com/huazai/archive/2008/11/25/1340911.html
第一種:最原始的JS驗證
HMTL代碼:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title></title>
<script language="javascript" type="text/javascript" src="CheckUserName.js"></script>
<script language="javascript">
</script>
</head>
<body>
<form id="form1" runat="server">
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td>Ajax Example:</td>
</tr>
<tr>
<td>
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td>Example 1:Check UserName IsExists</td>
</tr>
<tr>
<td>
<asp:Label ID="lblUserName" runat="server" Text="使用者名稱:"></asp:Label>
<input id="txtUserName" type="text" onblur="onBlur()"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
//CheckUserName.js
var ajax = function(option)
{
var request;
var createRequest = function()
{
var request;
if (window.XMLHttpRequest)
{
request = new XMLHttpRequest();
}
else
{
try
{
request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
request = new ActiveXObject("Msxml2.XMLHTTP");
}
}
return request;
}
var sendRequest = function()
{
request = createRequest();
// request.open("post", option.url, true);
// request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// request.onreadystatechange = ResponseRequest;
// request.send(option.param);
request.open("get", option.url+"?t=0", true);
request.onreadystatechange = ResponseRequest;
request.send(null);
}
var ResponseRequest = function()
{
if (request.readyState == 4)
{
if (request.status == 200)
{
option.Success(request);
}
else
{
option.Failure(request);
}
}
}
sendRequest();
}
var onBlur = function()
{
var option = { url: "Test.aspx", param: "t=1", Success:function(request){alert(request.responseText);}, Faitrue: function(request) { alert(false) } };
new ajax(option);
}
//Test.aspx
protected void Page_Load(object sender, EventArgs e)
{
if (Request["t"] != null)
{
this.Response.Clear();
string t = Request["t"].ToString();
if (t == "1")
{
Response.Write("使用者名稱已存在,請填寫其他的使用者名稱!");
}
else if (t == "0")
{
Response.Write("該使用者名稱沒被註冊,可以使用!");
}
this.Response.End();
}
}
第二種方式:利用JQuery架構ajax驗證
HMTL代碼:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script src="jquery-1.2.6.min.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
$("#Button1").click(function() {
$.ajax({
type: "get",
url: "ResponsePage.aspx?t=0",
dataType: 'html',
success: function(data) {
alert("post " + data);
},
error: function() { alert('error!'); }
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button1" type="button" value="顯示" />
</div>
</form>
</body>
</html>
//ResponsePage.apsx
protected void Page_Load(object sender, EventArgs e)
{
this.Response.Clear();
string t = Request["t"].ToString();
if (t == "1")
{
Response.Write("使用者名稱已存在,請填寫其他的使用者名稱!");
}
else if (t == "0")
{
Response.Write("該使用者名稱沒被註冊,可以使用!");
}
this.Response.End();
}