<html>
<head>
<title>I LOVE MAIYONGHAO!</title>
<script language = "JavaScript">
function validate(f){
if(!(/^\d+{5,15}$/.test(f.userId.value))){
alert("用户ID必须是5位到15位!");
f.userId.focus();
return false;
}
if(!(/^\w+{5,15}$/.test(f.userPass.value))){
alert("用户密码必须是5位到15位");
f.userPass.focus();
return false;
}
return true;
}
</script>
</head><body>
<form action = "../response/welcome.htm" method = "post" onSubmit = "return validate(this)">
账&nbsp;&nbsp;号:<input type = "text" name = "userId"><br/>
密&nbsp;&nbsp;码:<input type = "password" name = "userPass"><br/>
<input type = "submit" value = "登陆">
<input type = "reset" value = "重置">
</form>
</body>
</html>
为什么不能通过onSubmit来调用function进行判断呢???当我按下submit按钮的时候就会自动跳转到welcome.htm界面了!

解决方案 »

  1.   

    楼主这样写试试:return onSubmit("validate(this)");我以前都是这样用的就可以
      

  2.   

    在方法体中alert('---');一下~~~就知道进没进去了!!!
    然后在alert((/^\w+{5,15}$/.test(f.userPass.value)));
      

  3.   

    onSubmit = "return validate(this)"小写吧onsubmit另外你在你的validate第一行就写一个alert('aaa');看能弹出来不?  调试一下
    用firebug调试一下有什么错。f.userId.value  这么写可行么? 没试不过。最好是用 document.getElementById('');
      

  4.   

    你这个用editplus编辑就会发现
    当前脚本出现错误
    第6行和11行
    错误的数量词validate方法里面有语法错误
    把\d 和\w后面的加号去掉就好了
      

  5.   


    <html>
    <head>
        <title>I LOVE MAIYONGHAO!</title>
    <script language = "JavaScript">
        function validate(){
         alert("111") ;
         alert(document.getElementsByName("uID").value) ; // undefine
        }
    </script>
    </head><body>
        <form action = "../response/welcome.htm"  id ="form1" method = "post" >
        账&nbsp;&nbsp;号:<input type = "text" name = "userId" id ="uID"><br/>
        密&nbsp;&nbsp;码:<input type = "password" name = "userPass"><br/>
        <input type = "button" value = "登陆" onclick="validate()" />
        <input type = "submit" value = "登陆submit" onclick="validate()" />
        <input type = "reset" value = "重置">
        </form>
    </body>
    </html>也不知道什么原因,跟着学习学习
      

  6.   

    应该用alert(document.getElementById("uID").value) ;
    document.getElementsByName是取一组相同的name的,比如radio 和checkbox的
      

  7.   

    现在问题是不能进入那个validate()方法呢~~
      

  8.   

    在FORM 里面用 onsubmit="return 验证方法;" 就OK  
      

  9.   


    终于有机会纠正高手的错误了,在html中是不区分大小的
      

  10.   

    如果实在不行就换种方式吧<html>
    <head>
        <title>I LOVE MAIYONGHAO!</title>
    <script language = "JavaScript">
        function validate(f){
                      if(!(/^\d+{5,15}$/.test(f.userId.value))){
                alert("用户ID必须是5位到15位!");
                f.userId.focus();
                     return false
                            }
            if(!(/^\w+{5,15}$/.test(f.userPass.value))){
                alert("用户密码必须是5位到15位");
                f.userPass.focus();  
               return false
           }
               document.form1.submit();
     }
    </script>
    </head><body>
        <form action = "../response/welcome.htm" name="form1" method = "post" >
        账&nbsp;&nbsp;号:<input type = "text" name = "userId"><br/>
        密&nbsp;&nbsp;码:<input type = "password" name = "userPass"><br/>
        <input type = "button" value = "登陆" onSubmit = "return validate(this)">
        <input type = "reset" value = "重置">
        </form>
    </body>
    </html>
      

  11.   

    onSubmit = "return validate(this)"
    没分号
    改成onSubmit = "return validate(this);"
      

  12.   

    好结贴,感谢六楼的,确实是多了个"+"的问题,f.userId.value是没有错误的。感谢大家的回复!
      

  13.   

    另外onSubmit = "return validate(this);"这里可以不加分号的~