用js判断两次输入的密码是否相同。如果失败,希望留在原界面。求助
代码:<%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><...
代码:
<%@ page language="java" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>SSH2架构原型:注册</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<script type="text/javascript" charset=utf-8>
function check(){
var password = document.getElementById("password").value;
var repsword = document.getElementById("repsword").value;
if(password != repsword) {
alert("两次密码不同,请重新输入");
return false;
}
else {
return true;
}
}
</script>
</head>
<body>
<form action="${pageContext.request.contextPath}/register!register.do" method="post">
username:<input type="text" name="empname" /> <br />
password:<input type="password" name="password" id="password"/> <br />
repsword:<input type="password" name="repsword" id="repsword"/> <br />
<input type="submit" value="register" id="register" onclick ="check()"/>
<input type="reset" value="reset" />
<select name="userLevel">
<option value="0">管理账户</option>
<option value="1">普通用户</option>
</select>
</form>
</body>
</html> 展开
<%@ page language="java" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>SSH2架构原型:注册</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<script type="text/javascript" charset=utf-8>
function check(){
var password = document.getElementById("password").value;
var repsword = document.getElementById("repsword").value;
if(password != repsword) {
alert("两次密码不同,请重新输入");
return false;
}
else {
return true;
}
}
</script>
</head>
<body>
<form action="${pageContext.request.contextPath}/register!register.do" method="post">
username:<input type="text" name="empname" /> <br />
password:<input type="password" name="password" id="password"/> <br />
repsword:<input type="password" name="repsword" id="repsword"/> <br />
<input type="submit" value="register" id="register" onclick ="check()"/>
<input type="reset" value="reset" />
<select name="userLevel">
<option value="0">管理账户</option>
<option value="1">普通用户</option>
</select>
</form>
</body>
</html> 展开
4个回答
展开全部
check()函数调用不用写在行间。
js文件修改:阻止提交时的默认事件就可以了。document.getElementById('register') = function check(event){
var password = document.getElementById("password").value;
var repsword = document.getElementById("repsword").value;
if(password != repsword) {
alert("两次密码不同,请重新输入");
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
} else {
return true;
}
}
展开全部
onclick="return check()"
<script>
function check(){
var bl = true;
var password = document.getElementById("password").value;
var repsword = document.getElementById("repsword").value;
if(password != repsword ){
alert('两次密码输入不一致!');
bl = false;
}
return bl;
}
</script>
追问
您最上面的onclick="return check()"应该在哪里写?
我照下面的改后仍然跳转了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-09-08
展开全部
<form action="${pageContext.request.contextPath}/register!register.do" method="post" onsubmit="return check();">
username:<input type="text" name="empname" /> <br />
password:<input type="password" name="password" id="password"/> <br />
repsword:<input type="password" name="repsword" id="repsword"/> <br />
<input type="submit" value="register" id="register"/>
<input type="reset" value="reset" />
<select name="userLevel">
<option value="0">管理账户</option>
<option value="1">普通用户</option>
</select>
</form>
function check(){
var password = document.getElementById("password").value;
var repsword = document.getElementById("repsword").value;
if(password === ''){
alert('密码不能为空');
return false;
}
if(password != repsword) {
alert("两次密码不同,请重新输入");
return false;
}
}
追问
不正确
追答
form 我加了个onsubmit 事件
原来的按钮 的onclick事件 我去掉了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
大概意思,具体的自己改改
<input type="submit" value="register" id="register" onclick ="check()"/>
tyep改成【button】
js改成
if(password != repsword) {
alert("两次密码不同,请重新输入");
return false;
}
else {
submit();
}
<input type="submit" value="register" id="register" onclick ="check()"/>
tyep改成【button】
js改成
if(password != repsword) {
alert("两次密码不同,请重新输入");
return false;
}
else {
submit();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询