在form标签设置onsubmit属性,当return true之后才提交:
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<script>function judge() {var element=document.getElementById("username").value;if(element == ""){alert("用户名不能问空!");return false;}elsereturn true;}
</script>
<h1 align="center">注册页 面</h1>
<form action="RegisterValidate" method="post" onsubmit="return judge()">
<p align="center">
用 户 名:<input type="text" id="username" name="username" placeholder="输入20个字符以内">
</p>
<p align="center">
输入密码:<input type="text" name="password" placeholder="输入20个字符以内">
</p>
<p align="center">
确认密码:<input type="text" name="confirmPassword" placeholder="重新输入密码">
</p>
<p align="center">
<input type="submit" value="注册">
<input type="reset" value="重置">
</p>
</form>
</body>
</html>