最近项目进入尾期,主要给客户进行一些培训。客户在注册系统时每次会出现在填写完所有的信息后,在点击确定后,如果后台有此用户名,就添加不了,但页面上填写的信息可能就没了,比如密码项一定会清空。这是一个不人性化的地方,看网上好多论坛实现填写用户名后失去焦点后就可判断出是否可以填写.就在项目中添加此功能了~~
前台apx中加JS
<!--
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}
function checkUserName(){
var UserName = document.getElementById('tbxManagerName');
if (UserName.value == "")
return;
createXMLHttpRequest();
var url = "check_user.aspx?UserName=" +UserName.value;
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.send(null);
}
function handleStateChange(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
var isValid = xmlHttp.responseText;
var checkResult = document.getElementById("checkResult");
checkResult.innerHTML = (isValid == "true") ? "<IMG src= 'http://www.cnblogs.com/images/check_right.gif' align = 'absmiddle'> 恭喜,此用户可以注册!" : "<IMG src= 'http://www.cnblogs.com/images/check_error.gif' align = 'absmiddle'> 此用户名已存在,请选择基他用户名!";
}
}
}
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}
function checkUserName(){
var UserName = document.getElementById('tbxManagerName');
if (UserName.value == "")
return;
createXMLHttpRequest();
var url = "check_user.aspx?UserName=" +UserName.value;
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.send(null);
}
function handleStateChange(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
var isValid = xmlHttp.responseText;
var checkResult = document.getElementById("checkResult");
checkResult.innerHTML = (isValid == "true") ? "<IMG src= 'http://www.cnblogs.com/images/check_right.gif' align = 'absmiddle'> 恭喜,此用户可以注册!" : "<IMG src= 'http://www.cnblogs.com/images/check_error.gif' align = 'absmiddle'> 此用户名已存在,请选择基他用户名!";
}
}
}
后台check_user.aspx.cs
private void Page_Load(object sender, System.EventArgs e)
{
string UserName = Request.QueryString["UserName"];
// validate
bool isValid = false;
int count = 0;
count = (new BRL.Sys.ManagerBR()).GetResultByCondition(ManagerDS.MANAGER_TABLE,"Manager_LoginName = '"+UserName+"'");
if(count == 0)
isValid = true;
Response.Clear();
Response.Write(isValid ? "true" : "false");
Response.End();
}
{
string UserName = Request.QueryString["UserName"];
// validate
bool isValid = false;
int count = 0;
count = (new BRL.Sys.ManagerBR()).GetResultByCondition(ManagerDS.MANAGER_TABLE,"Manager_LoginName = '"+UserName+"'");
if(count == 0)
isValid = true;
Response.Clear();
Response.Write(isValid ? "true" : "false");
Response.End();
}
运行效果如下:
可以注册:
不可以注册: