下面标记的两个地方老是无法通过检查  if的方法里全都返回true 为什么还是调用alert呢  我日 崩溃了   <script language="javascript">
                var XmlHttp;   
                if(window.XMLHttpRequest)
                    XmlHttp=new window.XMLHttpRequest();
                else if(window.ActiveXObject)
                    XmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                    
        function window.onload()
       {
                     //if( chkname()&&chkpwd()&&chkre()&&chkmail()&&chkcode() )
       } 
       
        function chkinfo()
       {
   
             if( !chkpwd() )
            {
                alert( "请检查密码" )
               return; 
            }
            if( !chkname() )                             <B>这个地方</B>
            {
                alert( "请检查用户名" )
               return; 
            }            if( !chkre() )
             {
                alert( "请检查重复密码" )
               return; 
            }
            if( !chkmail() )                      <B>这个地方</B>
             {
                alert( "请检查邮箱" )
               return; 
            }
            if( !chkcode() )
             {
                alert( "请假查验证码" )
               return; 
            }            var posturl = "ajaxaction.aspx?op=reguser&txtname="+ document.getElementById( "txtname" ).value.Trim()+"&txtpwd="+ document.getElementById( "txtpwd" ).value.Trim()+"&txtmail="+ document.getElementById( "txtmail" ).value.Trim()+"&txtcode="+ document.getElementById( "txtcode" ).value.Trim()+"&r="+Math.random();
            XmlHttp.open("POST",posturl, true);
            XmlHttp.send(null);
            XmlHttp.onreadystatechange= function()
            {
                if (XmlHttp.readyState == 4 || XmlHttp.readyState == 'complete') 
                    if( XmlHttp.responseText==1 )
                    {
                        alert( "注册成功" );
                        window.location="default.aspx" ;
                    }                     else 
                    {
                        alert( "注册失败  "+XmlHttp.responseText )
                    } 
            }        } 
        
       function chkname()
       {
       
            if( document.getElementById( "txtname" ).value.Trim()=="" )
             {
                document.getElementById( "tdname" ).innerHTML="用户名不能为空";
               
                return false;
           } 
           if( document.getElementById( "txtname" ).value.Trim().length<6 )
            {
                document.getElementById( "tdname" ).innerHTML="用户名长度不能小于6位 不包括空格";
               
                return false;
            }
       //还要检查是否包含特殊字符 还有首字符是否为下划线或字母aaaaaaaaaaaaaaaaaaaa
                var posturl = "ajaxaction.aspx?op=unameexist&txtname="+ document.getElementById( "txtname" ).value.Trim()+"&r="+Math.random();
                XmlHttp.open("POST",posturl, true);
XmlHttp.send(null);
XmlHttp.onreadystatechange= function()
{
    if (XmlHttp.readyState == 4 || XmlHttp.readyState == 'complete') 
        if( XmlHttp.responseText=="0" )
       {
          document.getElementById( "tdname" ).innerHTML="" ;
         
          return true; 
       } 
        else 
       {
            document.getElementById( "tdname" ).innerHTML="用户名已被注册 请使用其他的用户名" ;
           
           return true; 
       } 
      else
            return true; 
}
       }       
       function chkpwd()
       {
        if( document.getElementById( "txtpwd" ).value.Trim().length<4 )
        {
             document.getElementById( "tdpwd" ).innerHTML="密码长度不能小于4位";
            return false;
        } 
       else
       {
        document.getElementById( "tdpwd" ).innerHTML="";
       
            return true;
       } 
       }
       
       function chkre()
       {
            if( document.getElementById( "txtpwd" ).value!=document.getElementById( "txtre" ).value )
            {
                document.getElementById( "tdre" ).innerHTML="请输入相同的密码";
                
                return false;
            }
            else
            {
            document.getElementById( "tdre" ).innerHTML="";
                return true;
            }
       }
       
       function chkmail()
       {
            if(! document.getElementById( "txtmail" ).value.Trim().isEmail() )
            {
                document.getElementById( "tdmail" ).innerHTML="请输入正确的邮箱地址";
                
                return false;
            }
            
            var posturl = "ajaxaction.aspx?op=emailexist&txtmail="+ document.getElementById( "txtmail" ).value.Trim()+"&r="+Math.random();
                XmlHttp.open("POST",posturl, true);
XmlHttp.send(null);
XmlHttp.onreadystatechange= function()
{
    if (XmlHttp.readyState == 4 || XmlHttp.readyState == 'complete') 
    
        if( XmlHttp.responseText!=0 )
        {
       
            document.getElementById( "tdmail" ).innerHTML="email已被注册 请使用其他的email" 
           
           return true;
           }
           else
           {
           document.getElementById( "tdmail" ).innerHTML="" 
           return true; 
           }
           
}
       }
       
       function chkcode()
       {
             if( document.getElementById( "txtcode" ).value.Trim()=="" )
            {
                document.getElementById( "tdcode" ).innerHTML="请输入验证码";
               
                return false;
            }
            else
            {
                document.getElementById( "tdcode" ).innerHTML="";
                return true;
            }
       }
    </script></head>
<body onload="document.form1.txtname.focus();">
    <form id="form1" runat="server">
        <table>
            <tr>
                <td>
                    用户名:</td>
                <td>
                    <input type=text id='txtname' name='txtname' onblur="return chkname()"  /></td>
                <td id="tdname">
                </td>
            </tr>
            <tr>
                <td>
                    密码:</td>
                <td>
                    <input id='txtpwd' name='txtpwd' onblur="return chkpwd()" type="password" /></td>
                <td id="tdpwd">
                </td>
            </tr>
            <tr>
                <td>
                    重复密码:</td>
                <td>
                    <input id='txtre' name='txtre' onblur=" return  chkre()" type="password" /></td>
                <td id="tdre">
                </td>
            </tr>
            <tr>
                <td>
                    密码找回邮箱:</td>
                <td>
                    <input id='txtmail' name='txtmail'  onblur="return chkmail()" /></td>
                <td  id="tdmail">
                </td>
            </tr>
            <tr>
                <td>
                    验证码:</td>
                <td>
                    <script type="text/javascript">document.write('<img id="MzImgExpPwd" alt="" src="VCode.aspx?temp='+ (new Date().getTime().toString(36)) +'" />');</script>
&nbsp;&nbsp; <input id="txtcode" tabIndex="5" type="text" name="txtcode" onblur="return chkcode()"> &nbsp;&nbsp;<A onclick="document.getElementById('MzImgExpPwd').src='VCode.aspx?temp='+ (new Date().getTime().toString(36)); return false"
href="#">看不清</A>
                <td id="tdcode">
                </td>
            </tr>
            <tr>
                <td colspan="2" align="right">
                    <button onclick='chkinfo()'>
                        注册</button></td>
            </tr>
        </table>
    </form>
</body>
</html>

解决方案 »

  1.   

    这2个函数要么返回false, 要么返回undefined,是ajax的callback函数返回true
      

  2.   

    function chkname()
           {
           
                if( document.getElementById( "txtname" ).value.Trim()=="" ) //JS里有Trim()方法么?晕···
                 {   
                    document.getElementById( "tdname" ).innerHTML="用户名不能为空";
                   
                    return false;
               }