0
我試圖驗證表單。只需嘗試確認密碼。 JS顯示警告和函數返回false,但它仍然提交表單到下一頁。即使js返回false,JSP也會提交表單
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script>
function validatePassword(){
var password=document.form1.password.value;
var cpassword=document.form1.cpassword.value;
if(password!=cpassword){
alert("Password Do not match");
return false;
}else{
return true;
}
}
</script>
</head>
<body>
<h1>Hello World!</h1>
<form name="form1" onsubmit="validatePassword();" action="login.jsp">
<table width="80%">
<tr>
<td style="text-align: center;width:40%">Password<font color="red">*</font>:</td>
<td style="text-align: center"><input type="text" name="password" required size="35"></td>
</tr>
<tr>
<td style="text-align: center">Confirm <font color="red">*</font>:</td>
<td style="text-align: center"><input type="text" name="cpassword" required size="35"></td>
</tr>
<input type="submit">
</form>
</body>
</html>
實際的事件處理程序不返回任何東西,應該是:'的onsubmit = 「返回validatePassword();」' – Teemu
@Teemu謝謝你的工作。沒有注意到! –