一个表单验证的小例子,
  说明:在点击提交时进行表单校验,具体要求如下:
1)用户名为3~16个字符,且不能包含”@”和”#”字符;
但是无论我在输入什么,都是提示用户名不能包含”@”或”#”字符。。
请大家帮忙看看哪里不对啊,在线等!   <html>
<head>
<script>



    function CheckForm(){

if( (document.userlogin.username.value="@") ||(document.userlogin.username.value="@")){
alert("用户名不能包含”@”或”#”字符");

document.userlogin.username.focus();
return false;
  }                                                               
     

}

</script>
</head>
<body > 


<form method="post" name="userlogin" onSubmit="return  CheckForm()">
    <table border="4" width="100">
<tr><td align="center"  colspan=2><nobr>用户注册信息</nobr></tr>
<tr><td align="left" ><p><nobr>用户名</nobr></td><td><input type="text" name="username" ></td></tr>
<tr><td align="left" ><p><nobr>密码</nobr></td><td><input type="text" name="password"></td></tr>
<tr><td align="left" ><p><nobr>校验密码</nobr></td><td><input type="text" name="checkpassword" ></td></tr>
<tr><td align="center" colspan="2"><input type="submit" value="提交">&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" ></td></tr>
</table>
</body>
</html>

解决方案 »

  1.   

    会不会是下面这段if( (document.userlogin.username.value="@") ||(document.userlogin.username.value="@")){ 
    alert("用户名不能包含”@”或”#”字符"); document.userlogin.username.focus(); 
    return false; 
      }  document.userlogin.username.value="@") 变成
    document.userlogin.username.value=="@") 
      

  2.   

    document.userlogin.username.value="@") 变成 
    document.userlogin.username.value.indexOf("@") < 0
      

  3.   

    document.userlogin.username.value.indexOf("@") > 0 说明有@
      

  4.   


    <html>
        <head>
            <script>            function CheckForm(){                if( (document.userlogin.username.value.indexOf("@")!=-1) ||(document.userlogin.username.value.indexOf("#")!=-1)){
                        alert("用户名不能包含”@”或”#”字符");                    document.userlogin.username.focus();
                        return false;
                    }            }        </script>
        </head>
        <body >
            <form method="post" name="userlogin" onSubmit="return  CheckForm()">
                <table border="4" width="100">
                    <tr> <td align="center"  colspan=2> <nobr>用户注册信息 </nobr> </tr>
                    <tr> <td align="left" > <p> <nobr>用户名 </nobr> </td> <td> <input type="text" name="username" > </td> </tr>
                    <tr> <td align="left" > <p> <nobr>密码 </nobr> </td> <td> <input type="text" name="password"> </td> </tr>
                    <tr> <td align="left" > <p> <nobr>校验密码 </nobr> </td> <td> <input type="text" name="checkpassword" > </td> </tr>
                    <tr> <td align="center" colspan="2"> <input type="submit" value="提交">&nbsp;&nbsp;&nbsp;&nbsp; <input type="reset" > </td> </tr>
                </table>
        </body>
    </html>