lan7229221 2018-12-27 19:16 采纳率: 100%
浏览 1991
已采纳

简单逻辑问题:注册的时候如果两次输入密码不同还是会提交from表单

 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>");
    }
%>
  • 写回答

4条回答 默认 最新

  • 禽兽丨雷 2018-12-27 12:36
    关注

    需要表单验证的情况下,button的type不要用submit,改成button,然后手动提交表单

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。