JSP中的javascript验证
这是我的jsp代码。我只想在客户端验证之后将表单提交到服务器。表单在提交时会转到指定的操作页面,但是,它应该先JSP中的javascript验证,javascript,forms,validation,jsp,Javascript,Forms,Validation,Jsp,这是我的jsp代码。我只想在客户端验证之后将表单提交到服务器。表单在提交时会转到指定的操作页面,但是,它应该先validate()it。我看不出我做错了什么 <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/h
validate()
it。我看不出我做错了什么
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="css/eoms.css" >
<title>Registration</title>
<script>
function validate()
{
var id = document.forms["RegForm"]["sID"];
var name = document.forms["RegForm"]["name"];
var des = document.forms["RegForm"]["des"];
var dept = document.forms["RegForm"]["depi"];
var pass = document.forms["RegForm"]["epw"];
var password = document.forms["RegForm"]["empw"];
if (id.value === "")
{
window.alert("Please enter your ID.");
id.focus();
return false;
}
if (name.value === "")
{
window.alert("Please enter your name");
name.focus();
return false;
}
if (des.value === "")
{
window.alert("Please enter your designation");
des.focus();
return false;
}
if (dept.value === "")
{
window.alert("Please enter your department.");
phone.focus();
return false;
}
if (pass.value !== password.value)
{
window.alert("Passwords must match.");
password.focus();
return false;
}
if (pass.value === "")
{
alert("Please enter your password.");
pass.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<div id="header">
<h3>
Employee<br>
Order<br>
Management<br>
System
</h3>
</div>
<div id="form" style="width: 30%; display: block; margin-left: 35%; margin-right: 35%; " >
<div style="text-align: center; background-color: #1E88E5; color: #BBDEFB; padding: 8px;">
Are you an employee?<br>
Register Here<br>
</div>
<div id="space">
<p>*All fields are required</p>
<%
try{
String regerror = session.getAttribute("regerror").toString();
out.println(regerror);
session.removeAttribute("regerror");
}
catch(Exception e){
}
%>
</div>
<br>
<table id="reg">
<form name="RegForm" action="addEmp.jsp" onsubmit="return validate()" method="post">
<tr>
<td>
Employee ID:
</td>
<td><input type="text" placeholder="Enter your ID" name="eID"></td>
</tr>
<tr>
<td>Employee Name:</td>
<td><input type="text" placeholder="Enter your Name" name="name"></td>
</tr>
<tr>
<td>
Designation
</td>
<td><input type="text" placeholder="Enter your designation" name="des"></td>
</tr>
<tr>
<td>
Department:
</td>
<td><input type="text" placeholder="Enter your department" name="dept"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" placeholder="Enter your password" name="epw"></td>
</tr>
<tr>
<td>Retype Password:</td>
<td><input type="password" placeholder="Re-type your password" name="empw"></td>
</tr>
<tr>
<td colspan="2" style="text-align:center;"><input type="submit" name="submit" value="Register"></td>
</tr>
</form>
<tr>
<td colspan="2" style="text-align:center;">Already a member? <a style="color: #424242;" href="login.jsp">Login</a></td>
</tr>
</table>
</div>
</body>
</html>
登记处
函数验证()
{
var id=document.forms[“RegForm”][“sID”];
var name=document.forms[“RegForm”][“name”];
var des=document.forms[“RegForm”][“des”];
var dept=文件格式[“RegForm”][“depi”];
var pass=document.forms[“RegForm”][“epw”];
var password=document.forms[“RegForm”][“empw”];
如果(id.value==“”)
{
window.alert(“请输入您的ID”);
id.focus();
返回false;
}
如果(name.value==“”)
{
window.alert(“请输入您的姓名”);
name.focus();
返回false;
}
如果(des.value==“”)
{
窗口提示(“请输入您的指定”);
des.focus();
返回false;
}
如果(部门值==“”)
{
窗口提示(“请输入您的部门”);
phone.focus();
返回false;
}
if(pass.value!==password.value)
{
警告(“密码必须匹配”);
password.focus();
返回false;
}
如果(pass.value==“”)
{
警报(“请输入您的密码”);
pass.focus();
返回false;
}
返回true;
}
员工
订单
管理
系统
你是员工吗?
在这里注册
*所有字段都是必填字段
员工ID:
员工姓名:
任命
部门:
密码:
重新键入密码:
已经是会员了?
抱歉,缩进错误。谢谢…你的
js
代码进入你的函数,但没有显示任何错误,因为你的js代码中有一些字段没有拼写,例如:没有输入名为sID
的输入,它是eID
,对于depi
它应该是dept
。工作代码:
函数验证(){
var id=document.forms[“RegForm”][“eID”];
var name=document.forms[“RegForm”][“name”];
var des=document.forms[“RegForm”][“des”];
var dept=文件格式[“注册表”][“部门”];
var pass=document.forms[“RegForm”][“epw”];
var password=document.forms[“RegForm”][“empw”];
如果(id.value==“”){
window.alert(“请输入您的ID”);
id.focus();
返回false;
}
如果(name.value==“”){
window.alert(“请输入您的姓名”);
name.focus();
返回false;
}
如果(des.value==“”){
窗口提示(“请输入您的指定”);
des.focus();
返回false;
}
如果(部门值==“”){
窗口提示(“请输入您的部门”);
部门焦点();
返回false;
}
if(pass.value!==password.value){
警告(“密码必须匹配”);
password.focus();
返回false;
}
如果(pass.value==“”){
警报(“请输入您的密码”);
pass.focus();
返回false;
}
返回true;
}
你是员工吗?
在这里注册
*所有字段都是必填字段
员工ID:
员工姓名:
任命
部门:
密码:
重新键入密码:
已经是会员了?
你的js
代码进入你的函数,但没有显示任何错误,因为你的js代码中有一些字段没有拼写,例如:没有输入名称sID
它是eID
,对于depi
它应该是dept
。工作代码:
函数验证(){
var id=document.forms[“RegForm”][“eID”];
var name=document.forms[“RegForm”][“name”];
var des=document.forms[“RegForm”][“des”];
var dept=文件格式[“注册表”][“部门”];
var pass=document.forms[“RegForm”][“epw”];
var password=document.forms[“RegForm”][“empw”];
如果(id.value==“”){
window.alert(“请输入您的ID”);
id.focus();
返回false;
}
如果(name.value==“”){
window.alert(“请输入您的姓名”);
name.focus();
返回false;
}
如果(des.value==“”){
窗口提示(“请输入您的指定”);
des.focus();
返回false;
}
如果(部门值==“”){
window.alert(“Pl
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="css/eoms.css" >
<title>Registration</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
<script>
$(document).ready(function (){
$("form").submit(function (){
var id = $("#eid").val();
var employname = $("#ename").val();
var designation = $("#desi").val();
var department = $("#dep").val();
var password = $("#password").val();
var passmact = $("#passm").val();
if(id===''){
alert("Please enter id");
id.focus();
return false;
}
if(employname ===''){
alert("Please enter employname");
return false;
}
if(designation===''){
alert("Please enter desinatin");
return false;
}
if(department ===''){
alert("Please enter despa");
return false;
}
if(password===''){
alert("Please enter pass");
return false;
}
if(password!==passmact){
alert("Password not match");
return false;
}
return true;
});
});
</script>
</head>
<body>
<div id="header">
<h3>
Employee<br>
Order<br>
Management<br>
System
</h3>
</div>
<div id="form" style="width: 30%; display: block; margin-left: 35%; margin-right:
35%; " >
<div style="text-align: center; background-color: #1E88E5; color: #BBDEFB;
padding: 8px;">
Are you an employee?<br>
Register Here<br>
</div>
<div id="space">
<p>*All fields are required</p>
<%
try{
String regerror =
session.getAttribute("regerror").toString();
out.println(regerror);
session.removeAttribute("regerror");
}
catch(Exception e){
}
%>
</div>
<br>
<!-- <p id="error"></p>-->
<table id="reg">
<form name="RegForm" action="addEmp.jsp" method="post">
<tr>
<td>
Employee ID:
</td>
<td><input type="text" id="eid" placeholder="Enter your ID"
name="eID"></td>
<p id="errorid"></p>
</tr>
<tr>
<td>Employee Name:</td>
<td><input type="text" id="ename" placeholder="Enter your Name"
name="name"></td>
<p id="erroremploy"></p>
</tr>
<tr>
<td>
Designation
</td>
<td><input type="text" id="desi" placeholder="Enter your
designation" name="des"></td>
<p id="errordes"></p>
</tr>
<tr>
<td>
Department:
</td>
<td><input type="text" id="dep" placeholder="Enter your
department" name="dept"></td>
<p id="errordep"></p>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" id="password" placeholder="Enter your
password" name="epw"></td>
<p id="errorpass"></p>
</tr>
<tr>
<td>Retype Password:</td>
<td><input type="password" id="passm" placeholder="Re-type your
password" name="empw"></td>
<p id="errorpmatch"></p>
</tr>
<tr>
<td colspan="2" style="text-align:center;"><input id="btns"
type="submit" name="submit" value="Register"></td>
</tr>
</form>
<tr>
<td colspan="2" style="text-
align:center;">Already a member? <a style="color: #424242;" href="login.jsp">Login</a>
</td>
</tr>
</table>
</div>
</body>
</html>