JSP验证函数为什么不执行? 代码如下!!!
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="JavaScript">
<!--
function check(){//验证数据的合法性
alert('test');
if(form1.username.value == ""){
alert("用户名不能为空,请输入用户名!");
form1.username.focus();
return false;
}
if(form1.userpassword.value == ""){
alert("用户密码不能为空,请输入密码!");
form1.userpassword.focus();
return false;
}
if(form1.reuserpassword.value == ""){
alert("用户确认密码不能为空,请输入密码!");
form1.reuserpassword.focus();
return false;
}
if(form1.userpassword.value != form1.reuserpassword.value){
alert("密码与确认密码不同!");
form1.password.focus();
return false;
}
if(form1.email.value.length != 0){
for(i=0;i<form1.email.value.length;i++)
if(form1.email.value.charAt(i)=="@")break;
if(i==form1.email.value.length){
alert("非法的E-Mail地址!");
form1.email.focus();
return false;
}
}else{
alert("请输入E-Mail!");
form1.email.focus();
return false;
}
return true;
}
//-->
</script>
<title>新用户注册</title>
<base href="<%=basePath%>">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<form name="form1" action="acceptuserRegist.jsp" method="post"
onsubmit="return check()">
新用户注册
<br>
用户名(*):
<input type="text" name="username" size="20">
<br>
密 码(*):
<input type="password" name="userpassword" size="20">
<br>
再输一次密码(*):
<input type="password" name="userpassword" size="20">
<br>
<input type="submit" value="提交" name="B1" onclick="check()">
<input type="reset" value="全部重写" name="B2">
<Br>
</form>
</body>
</html>
答案:if(form1.userpassword.value == ""){
alert("用户密码不能为空,请输入密码!");
form1.userpassword.focus();
return false;
}//这个括号是中文的,你改成英文的就行了 }
另,调JS建议用firefox浏览器,装上firebug插件。
上一个:谁有jsp+javaBean访问access数据库的代码啊 ,能执行出来的,编了好几天总是出错,我快绝望了!!!
下一个:jsp中怎么实现一个表单中放两个按钮,这是我的代码 不知道哪里有问题~