1. 程式人生 > >form表單當滿足條件之後才提交表單的方法

form表單當滿足條件之後才提交表單的方法

使用form表單的onsubmit屬性,當屬性值為true時,則提交,當屬性值為false時,則不提交:

<%@ 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 username = document.getElementById("username").value;
		var pwd = document.getElementById("password").value;
		var conpwd = document.getElementById("confirmPassword").value; 
		if(username == "")
		{
			alert("使用者名稱不能問空!");
			return false;
		}
		else if(pwd!=conpwd)
		{
			alert("兩次輸入的密碼不一致!");
			return false;
		}
		else
			return true;
	}
</script>
<h1 align="center">註冊頁	面</h1>
<form action="RegisterValidate" method="post" onsubmit="return judge()">
<p align="center">
用&nbsp;&nbsp;戶&nbsp;&nbsp;名:<input type="text" id="username" name="username" placeholder="輸入20個字元以內">
</p>
<p align="center">
輸入密碼:<input type="password" name="password" id="password" placeholder="輸入20個字元以內">
</p>
<p align="center">
確認密碼:<input type="password" name="confirmPassword" id="confirmPassword" placeholder="重新輸入密碼">
</p>
<p align="center">
<input type="submit" value="註冊">
<input type="reset" value="重置">
</p>
</form>
</body>
</html>