function login1(){
var name=document.getElementById("name").value;
var pwd=document.getElementById("pwd").value;
var vri=document.getElementById("verify").value;
var code=document.getElementById("code").innerHTML;
var againpwd=document.getElementById("againpwd").value;
if(vri==code){
if(name=="" || pwd==""){
alert("登录名和密码不能为空,请输入");
return false;
if(pwd!=againpwd){
alert("两次输入密码不同");
return false;
}
}
}else{
alert("验证码不正确");
return false;
}
}
注册的时候如果两次输入密码不同还是会提交from表单
自己是在时找不出问题,求大神解答下
12.28更新整个页面代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>游戏公园 - 注册页面</title>
<link rel="stylesheet" href="Public/Home/css/index.css" type="text/css"></link>
<script>
function code(){
var str="qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM";
var str1=0;
for(var i=0; i<4;i++){
str1+=str.charAt(Math.floor(Math.random()*62))
}
str1=str1.substring(1)
document.write("<span onClick='code()' id='code' style='display: inline-block; width: 100px;height: 46px;vertical-align: middle;background-color: black;font-size: 25px;color: #fff;text-align: center;line-height: 46px'>"+str1+"</span>");
}
function bg(){
document.getElementById("name").style.backgroundColor="#EEE";
}
function bbg(){
document.getElementById("name").style.backgroundColor="#FFF";
}
function bg1(){
document.getElementById("pwd").style.backgroundColor="#EEE";
}
function bbg1(){
document.getElementById("pwd").style.backgroundColor="#FFF";
}
function bg2(){
document.getElementById("verify").style.backgroundColor="#EEE";
}
function bbg2(){
document.getElementById("verify").style.backgroundColor="#FFF";
}
function login1(){
var name=document.getElementById("name").value;
var pwd=document.getElementById("pwd").value;
var vri=document.getElementById("verify").value;
var code=document.getElementById("code").innerHTML;
var againpwd=document.getElementById("againpwd").value;
if(vri==code){
if(name=="" || pwd==""){
alert("登录名和密码不能为空,请输入");
return false;
if(pwd!=againpwd){
alert("两次输入密码不同");
return false;
}
}
}else{
alert("验证码不正确");
return false;
}
}
</script>
</head>
<body>
<header> <div href="../../index.jsp" class="logo" style="background:url('Public/Home/img/logo1.png');width:180px;height:60px;no-repeat;padding-right:5px"></div>
<div class="desc">欢迎注册</div>
</header>
<section>
<form method="post" action="control/register.jsp"
onSubmit="return login1()">
<div class="register-box">
<label for="username" class="username_label">用 户 名 <input
maxlength="20" type="text" id="name" v onFocus="bg()" onBlur="bbg()"
name="name" placeholder="您的用户名或登录名"> </label>
<div class="tips"></div>
</div>
<div class="register-box">
<label for="username" class="other_label">设 置 密 码 <input
maxlength="20" type="password" onFocus="bg1()" onBlur="bbg1()"
id="pwd" name="password" placeholder="建议至少使用两种字符组合"> </label>
<div class="tips"></div>
</div>
<div class="register-box">
<label for="username" class="other_label">确 认 密 码 <input
maxlength="20" type="password" id="againpwd" name="againpassword"
placeholder="请再次输入密码"> </label>
<div class="tips"></div>
</div>
<div class="register-box">
<label for="username" class="other_label">验 证 码 <input
style="height:30px" cursor:pointer id="verify" type="text"
onFocus="bg2()" onBlur="bbg2()" name="verify" maxlength="20"
type="text" placeholder="请输入验证码"> <script>code()</script>
</label>
<div class="tips"></div>
</div>
<div class="submit_btn">
<button type="submit" id="submit_btn" onClick="login()">立 即
注 册</button>
</div>
</form>
</section>
<script src="js/index.js" type="text/javascript" charset="utf-8"></script>
<div
style="text-align:center;margin:50px 0; font:normal 14px/24px 'MicroSoft YaHei';">
</div>
</body>
</html>
control下jsp代码如下
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="db.jdbc"%>
<%@ page import="java.sql.*"%>
<%
String username = new String(request.getParameter("name").getBytes(
"iso-8859-1"), "UTF-8");
String password = request.getParameter("password");
jdbc j = new jdbc();
String sql = "insert into login(username,password) values('"
+ username + "','" + password + "') ";
String selectsql = "select * from login where username='"
+ username + "'";
ResultSet rs = j.exquery(selectsql);
if (rs.next()) {
request.getSession().setAttribute("username", username);
out.print("<script>alert('该用户已存在');location.href='../register.jsp'</script>");
} else if (j.UID(sql)) {
out.print("<script>alert('注册成功');location.href='../login.jsp'</script>");
} else {
out.print("<script>alert('注册失败');location.href='../register.jsp'</script>");
}
%>