<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)">
账 号:<input type = "text" name = "userId"><br/>
密 码:<input type = "password" name = "userPass"><br/>
<input type = "submit" value = "登陆">
<input type = "reset" value = "重置">
</form>
</body>
</html>
为什么不能通过onSubmit来调用function进行判断呢???当我按下submit按钮的时候就会自动跳转到welcome.htm界面了!
解决方案 »
- document.getElementsByName("type1").value 得不到值?
- java集合问题,请大家帮帮忙,有此类问题告诉我!谢谢
- spring mvc映射默认路径
- 使用Hibernate时遇到的一个小问题,高手请赐教!
- spring依赖注入、接口和实现类
- 上哪买一个好一点的外游服务器呢?
- 刷新重复提交---特殊问题
- 服务端传递数据给客户端问题
- 100分!关于怎么封装Jdbc连接oracle存储过程
- 头疼!!如何在jbuilder中通过点击按钮同时向两个表添加记录?
- getHibernateTemplate()与getSession()区别?
- 【急!!】JXL如何修改Excel文件中指定单元格的内容???
然后在alert((/^\w+{5,15}$/.test(f.userPass.value)));
用firebug调试一下有什么错。f.userId.value 这么写可行么? 没试不过。最好是用 document.getElementById('');
当前脚本出现错误
第6行和11行
错误的数量词validate方法里面有语法错误
把\d 和\w后面的加号去掉就好了
<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" >
账 号:<input type = "text" name = "userId" id ="uID"><br/>
密 码:<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>也不知道什么原因,跟着学习学习
document.getElementsByName是取一组相同的name的,比如radio 和checkbox的
终于有机会纠正高手的错误了,在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" >
账 号:<input type = "text" name = "userId"><br/>
密 码:<input type = "password" name = "userPass"><br/>
<input type = "button" value = "登陆" onSubmit = "return validate(this)">
<input type = "reset" value = "重置">
</form>
</body>
</html>
没分号
改成onSubmit = "return validate(this);"