我一个js表单验证不通过时,点登陆会提示,但还是会提交。怎么修改代码才能验证不通过就不提交?
这是详细的代码:<%@pagelanguage="java"import="java.sql.*"pageEncoding="UTF-8"%><html><head><t...
这是详细的代码:
<%@ page language="java" import="java.sql.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>登陆</title>
</head>
<body>
<script language = "javascript" type="text/javascript">
function check(){
if(f1.username.value == "")
{
alert("用户名为空!");
form.username.focus();
return false;
}
if(f1.password.value == "")
{
alert("请输入密码!");
form.password.focus();
return false;
}
return true;
}
</script>
<center>
<h1>欢迎来到论坛!</h1><br>
<form name = "f1" action = "login.jsp" method="post" onsubmit = "return check()">
<table border="1">
<tr>
<td width="30%"><center>用户名:</center></td>
<td width="70%"><input type="text" name="username"></td>
</tr>
<tr>
<td><center>密码:</center></td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan = "2">
<center>
<input type="submit" name = "submit" value="登录">
<input type="button" value="注册" onclick = "window.location.href='zhuce.jsp'">
</center>
</td>
</tr>
</table>
</form>
</center>
</body>
</html> 展开
<%@ page language="java" import="java.sql.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>登陆</title>
</head>
<body>
<script language = "javascript" type="text/javascript">
function check(){
if(f1.username.value == "")
{
alert("用户名为空!");
form.username.focus();
return false;
}
if(f1.password.value == "")
{
alert("请输入密码!");
form.password.focus();
return false;
}
return true;
}
</script>
<center>
<h1>欢迎来到论坛!</h1><br>
<form name = "f1" action = "login.jsp" method="post" onsubmit = "return check()">
<table border="1">
<tr>
<td width="30%"><center>用户名:</center></td>
<td width="70%"><input type="text" name="username"></td>
</tr>
<tr>
<td><center>密码:</center></td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan = "2">
<center>
<input type="submit" name = "submit" value="登录">
<input type="button" value="注册" onclick = "window.location.href='zhuce.jsp'">
</center>
</td>
</tr>
</table>
</form>
</center>
</body>
</html> 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询