为了尽量减少传输的数据,创建一个webservice,我叫:verify.asmx, 添加一个WebMethod:
[WebMethod]
public string CheckSignupUserid( string strUserId )
{
.....
//如果用户名有效,返回空串
//否则返回建议或者指示文字
String strResult = String.Empty;
if ( objReader.HasRows )
{
strResult="对不起,用户名 《<font color='red' style='font-weight:bold'>"+strUserId+"</font>》 已经被人注册了!<br>请选择其他的用户名,推荐:《"+strUserId+"2006》";
}
return strResult;
}
另外,使用microsoft的webservice组件调用webservice, 下载地址请自己查:webservice.htc
在aspx文件中创建一个div
<div id="service" style="BEHAVIOR: url(/ccmall/Service/webservice.htc)" onresult="onVerifyResult()"></div>
当然,aspx中输入用户名的地方也要有啊 :
<input style="FONT-SIZE: 12px; WIDTH: 180px" type="text" name="user_id" size="20" class=SmallTextBox onblur="checkUserId()" onfocus="onFocusCommon(this)"> <font color=#666666>2-10个字符,可以是字母、数字或中文</font>
<div id="dvVerifyUser" style="DISPLAY:none; BACKGROUND-COLOR:#ff9900"></div>
创建一段js代码:
<script language="javascript">
var iVerifyUserCall=0;
function init() {
service.useService("./Service/Verify.asmx?WSDL", "VerifyService");
}
function onVerifyResult() {
if ( (!event.result.error)&&(iVerifyUserCall==event.result.id) ) {
err_id = 0;
if ( event.result.value != "" ) {
document.getElementById("user_id").style.background = "#ff0000";
document.getElementById("dvVerifyUser").innerHTML = event.result.value;
document.getElementById("dvVerifyUser").style.display = "";
}
} else {
//alert("something else fired the event!");
alert("这个站点出了一些 小毛病, 嘻嘻,不过你可以不管它!have fun!");
}
}
function checkUserId() {
iVerifyUserCall = service.VerifyService.callService("CheckSignupUserid", document.getElementById("user_id").value);
}
这样一来, 就可以在输入用户名后,后台自动判断该用户名是否有效, 从而不需要用按钮提交后再告诉用户出错信息!
ps: 本文的实现用在本人的一个项目实现,其中代码参考了网络资源, 参考的时候并没有想着写到这里,是因为现在无法删除blog,我才在这里编辑了原来的文本,所以,无法把原来参考的资源的url给出,请原谅!