验证用户名是否有重复的代码

 

客户端:

    function ValidateUser()
    
{
        
//alert("hello");
        var obj = document.getElementById("lblCheckUser");
        
var account = document.getElementById("txtAccount").value;
        
if(account.length < 3)
        
{
            obj.innerHTML 
= '<span style="vertical-align:middle; text-align:center"><img src="../images/check_error.gif" width="12" height="12"> ' + "对不起,您输入的帐号小于3个字符, 请输入一个较长的用户名。</span>";
            
return;
        }

        
var chk = true;
        
        
if(chk) {
            
var username = escape(account);
            
var url = "CheckUser.aspx?name=" + account + "&t=" + Math.random(); //加参数t是为了保证URL不同(防止两次传的URL一样,js就不会从数据库中查找,直接从缓存中查找)
            //alert(url);
            var sta = GetResponseText(url); 
            
//alert(sta);
            
            
//异步传输失败
            if(sta == false
            
{
                chk 
= true;
                obj.className 
= "d_ok";
                obj.innerHTML 
= '未能检测注册,可以尝试注册!';
             }
//if(sta == false)
             //异步传输成功
             else 
             
{
                
if(sta.indexOf("regok"!= -1
                
{
                    
//验证通过
                    chk = true;
                    obj.className 
= "Right";
                    obj.innerHTML 
=  '<span style="vertical-align:middle; text-align:center"><img src="../images/check_right.gif" width="12" height="12"> ' + "该帐号可以注册!</span>";;
                   
                 }

                 
else
                 
{
                    chk 
= false;
                    obj.className 
= "Error";
                    obj.innerHTML 
= '<span style="vertical-align:middle; text-align:center"><img src="../images/check_error.gif" width="12" height="12"> ' + "该帐号已经被注册,请改名后重试!</span>";
                  }
 
              }
// else         
          }
// if(chk)
          else
          
{
            obj.className 
= "Error";
          }
//end else       
          return chk;
    }


    
function GetResponseText(url)
    
{
        
var http_request;
    
//获取XMLHttpRequest对象
        if(window.XMLHttpRequest)
        
{
            http_request 
= new XMLHttpRequest();
            
if(http_request.overrideMimeType)
            
{
                http_request.overrideMimeType(
'text/xml');
            }

        }
//if(window.XMLHttpRequest)
        else if (window.ActiveXObject)
        
{
            
try
            
{
                http_request 
= new ActiveXObject("Msxml2.XMLHTTP");
            }

            
catch (e)
            
{
                
try
                
{
                    http_request 
= new ActiveXObject("Microsoft.XMLHTTP");
                }

                
catch(e)
                
{
                }

            }

        }
//else if (window.ActiveXObject)
        
    
//发送请求
        http_request.open('GET', url, false);
        http_request.send(
null);
        
if(http_request.readyState == 4)
        
{
            
if(http_request.status == 200)
            
{
                
var requestdoc = http_request.ResponseText;
            }

            
else
            
{
                requestdoc 
= "error";
            }

        }

        
        
return requestdoc;
        
    }
//end GetResponseText(url)

服务端代码:

        string nam = Request.QueryString["name"].ToString(); //获取URL中name参数值
        BLAdmin admin = DalFactory.Factory.GetBLAdmin();
        bool isExist = admin.hasSameName(nam); //查找数据库中是否有相同的用户名
        if (isExist)
        {
            Response.Write("Error");
        }
        else
        {
            Response.Write("regok");
        }

posted @ 2008-12-23 10:39  earthworm  阅读(444)  评论(0编辑  收藏  举报