js 验证密码和用户名是否为空,但还可以跳转即使密码为空

meluo 发布于 2014/03/31 18:43
阅读 3K+
收藏 0

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<jsp:useBean id="user" scope="request" class="mrluo.User"/>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>用户登陆</title>
        <link href="layout.css" rel="stylesheet" type="text/css" />
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">
      <SCRIPT type="text/javascript">
         function kong(form){
             var name=form.name.value;
             var password=form.password.value;
             if(name=="" ||password="")
             {
                 alert("请输入用户名和密码!");
                 return false;
             }
             else{
                 alert("请输入用户名和密码!");
                 return true;
             }
         }
           
       </SCRIPT>
    </head>
    <body>
        <div id="denglu">
            <form onsubmit=="return kong(this.form)();" method="post" action="Guoliu" name="deng">
                <br>
               
                <table align="center">
                   
                    <caption>用户登录</caption>
                    <tr>
                          <td>用户:</td>
                        <td><input type="text" name="name" value="" /></td>
                     
                    </tr>
                    <tr>
                        <td>密码:</td>
                        <td><input type="password" name="password" value="" /></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="right">
                            <input type="submit" value="登陆" name="submit" id="submit" class="bj"/>
                            <input type="reset" value="重置" name="reset" class="bj" />
                            &nbsp;&nbsp;
                        </td>
                      
                    </tr>
                </table>
               
            </form>
        </div>
    </body>
</html>
    为什么即使密码为空还可以登陆成功,

加载中
0
纠结名字

千万不要给表单元素起个name或者id为submit,否则后果自负。

return kong(this.form)();你这是个什么玩意?

0
ththththht
ththththht
password=“” 少了个等号。password被赋空值,判断时转换为boolean false。所以能通过。你判断应该是password是否有值,而不是是否没值。if (name&&password)
0
H
HopenetOSC

if(name=="" ||password="")
             {
                 alert("请输入用户名和密码!");
                 return false;
             }
             else{
                 alert("请输入用户名和密码!");
                 return true;

             }


先不说你的语法错误。。。逻辑好像不对吧???

if(用户名等于空 || 密码等于空){

    弹窗错误提示

    return false;

}else{

    验证得到用户名和密码都不为空。。。

    为何又要提示请输入用户名和密码???

    然后返回验证通过的:return true;

}


修改:

if(name == "" || password == ""){

    alert("用户名或密码不能为空,请检查输入");

    return false;

}else { return true; }


2.form的js触发事件对吗?反正我没这么写过。

<form onsubmit=="return kong(this.form)();"  ????

我只这样写过。。。

<form onsubmit="return kong(this.form);"










返回顶部
顶部