JavaScript 不能在 ECLIPSE IDE 中使用 JSP 页面



在此处输入图像描述

这是学生注册的表格,它收集信息并将其发送到"操作"中提到的Servlet "StudentRegisterSrv"。 但是用于密码确认匹配的JS代码不起作用。我已经尝试了内部(在同一文件上(和外部JS(通过链接(。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Register | Home</title>
<script type="text/javascript">
function checkPassword(form) { 
password1 = form.password.value; 
password2 = form.password2.value; 
// If password not entered 
if (password == '') 
alert ("Please enter Password"); 
// If confirm password not entered 
else if (password2 == '') 
alert ("Please enter confirm password");                       
// If Not same return False.     
else if (password != password2) { 
alert ("nPassword did not match: Please try again...") 
return false; 
}  
// If same return True. 
else{ 
alert("Password Match: Welcome to GeeksforGeeks!") 
return true; 
} 
} 
</script>            
<link rel="stylesheet" href="css/body.css">
</head>
<body>
<h1>Student Registration</h1>
<form action="StudentRegisterSrv" method="post">
<input type="text" name="name" placeholder="Name" required>  <br><br> 
<input type="email" name="email" placeholder="Email" required>  <br> <br> 
<input type="number" name="phone" placeholder="Mobile No." required>    <br><br> 
<input id="pwd1" type="password" name="password" placeholder="Password" required>   <br><br>    
<input id="pwd2" type="password" name="password2" placeholder="Password Again" required>    <br><br>
<input type="submit" value="Register" onSubmit = "return checkPassword(this)"> 
<input type="reset" value=" RESET">
</form>
</body>
</html>

您需要将onSubmit="return checkPassword()"添加到表单标签中,以便在单击提交按钮时调用该按钮,如果false将从js返回,则表单将不会提交,否则如果表单将提交true则表单将提交。此外,我还使用document.getElementById("pwd1").value来获取密码字段的值和固定的拼写错误。

演示代码

function checkPassword() {
//get value by id
var password1 = document.getElementById("pwd1").value;
var password2 = document.getElementById("pwd2").value;
// If password not entered 
if (password1 == '')
alert("Please enter Password");
// If confirm password not entered 
else if (password2 == '')
alert("Please enter confirm password");
// If Not same return False.     
else if (password1 != password2) {
alert("nPassword did not match: Please try again...")
return false; //will return false and prevent form to submit
}
// If same return True. 
else {
alert("Password Match: Welcome to GeeksforGeeks!")
return true; //will return true and submit the form
}
}
<h1>Student Registration</h1>
<form name="form" action="StudentRegisterSrv" method="post" onSubmit="return checkPassword()">
<input type="text" name="name" placeholder="Name" required> <br><br>
<input type="email" name="email" placeholder="Email" required> <br> <br>
<input type="number" name="phone" placeholder="Mobile No." required> <br><br>
<input id="pwd1" type="password" name="password" placeholder="Password" required> <br><br>
<input id="pwd2" type="password" name="password2" placeholder="Password Again" required> <br><br>
<input type="submit" value="Register">
<input type="reset" value=" RESET">
</form>

最新更新