第一次用javascript写了一个不用刷新就能实现--用户名验证的例子

<script type="text/javascript" >
        
var xmlHttp;
        
function createXMLHttpRequest()
        
{
            xmlHttp 
= false
            
if(window.XMLHttpRequest)//开始初始化XMLHttpRequest对象//Mozilla浏览器
            {
                xmlHttp 
= new XMLHttpRequest();
                
if(xmlHttp.overrideMimeType) //设置Mime类别
                {           
                    xmlHttp.overrideMimeType(
"text/xml");
                }

            }

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

                
catch(e)
                
{
                    
try
                    
{
                        xmlHttp 
= new ActiveXObject("Micosoft.XMLHTTP");
                    }

                    
catch(e)
                    
{
                    }

                }

            }

            
if(!xmlHttp)
            
{
                
//异常,创建实例失败
                window.alert("不能创建XMLHttpRequest对象实例");
                
return false;
            }

        }


        
function CheckUserNameURL(url)
        
{
            createXMLHttpRequest();
            xmlHttp.onreadystatechange 
= processRequest;
            xmlHttp.open(
"POST",url,true);
            xmlHttp.send(
null);
        }


        
//处理返回信息的函数
        function processRequest()
        
{
            
if(xmlHttp.readyState == 4)//判断对象状态
            {
                
if(xmlHttp.status == 200)//信息已经成功返回,开始处理信息
                
                    
var aa = xmlHttp.responseText;
                    
var bb = aa.substr(0,1);
                    
if(bb == 0)
                    
{
                        alert(
"可以注册");
                    }

                    
else if(bb == 1)
                    
{
                        alert(
"用户已经存在,请选择其它用户名称");
                    }

                    
else
                    
{
                        alert(
"出现错误");
                    }

                }

                
else
                
{
                    
//页面不正常
                    alert("你所请求的页面有异常");
                }

            }

        }


        
function userCheck()
        
{
            
var userName = document.getElementById("userName").value;
            
if(userName == "")
            
{
                alert(
"用户名不能为空");
                document.getElementById(
"userName").focus();
                
return false;
            }

            
else
            
{
                CheckUserNameURL(
"logon.aspx?userName=" + userName );
            }

        }

        
</script>

上面这段代码是用来对用户进行判断,如果已经注册了,那么就会弹出一个对话框

说"用户已经存在,请选择其它用户名称"

如果不存在就提示"可以注册";

下面是webForm1.aspx的部门代码,

                <TABLE id="Table1" style="WIDTH: 408px; HEIGHT: 99px" cellSpacing="1" cellPadding="1" width="408"
                    border
="0">
                    
<TR>
                        
<TD align="right">用户名:</TD>
                        
<TD><asp:textbox id="userName" runat="server"></asp:textbox>
                            
<INPUT id="checkUsername" type="button" value="验证" onmousemove="userCheck();">
                        
</TD>
                    
<TR>
                        
<TD align="right">密 码:</TD>
                        
<TD><asp:textbox id="txtPassword" runat="server"></asp:textbox></TD>
                    
</TR>
                    
<TR>
                        
<TD align="right"></TD>
                        
<TD>
                            
<asp:Button id="btnCheckUser" runat="server" Text="验  证"></asp:Button>
                            
<DIV></DIV>
                        
</TD>
                    
</TR>
                
</TABLE>

下面是logon.aspx.cs的代码,这段代码放在page_load里就行了

            string userName = "";
            
try
            
{
                userName 
= this.Request.QueryString["userName"].ToString();
                
string sqlString = "SELECT COUNT(*) FROM Users WHERE userName ='" + userName + "'";
                
string count = SetConnection.ExecuteScalar(sqlString );
                
this.Response.Write(count);
                
this.lblMessage.Text = count ;
            }

            
catch
            
{
            }

 

--------------------------------------------
本来是想把 javaScript 代码放到一个.js的代码里,可是不知道为什么一放到去然后在title里引用时老是出错,不知道是什么原因.

在写这个验证当中遇到的最大的困难就是,xmlHttp.responseText 返回来的字符串如何把第一个字取回来,弄了老久,今天看到可以用string.substr()这个方法,解决了上面问题.

----

posted on 2006-10-13 11:46  小土泥  阅读(293)  评论(0编辑  收藏  举报

导航