这是一段修改密码的页面代码<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>修改密码</title>
<LINK href="../style/adminCss.css" type=text/css rel=stylesheet>
<script type="text/javascript">
function check(){
var oldPassword=document.myform.oldpassword.value;  //旧密码
var nowPassword=(String)session.getAttribute("loginPwd"); //这里是登陆时记录的密码
var password=document.myform.password.value;     //新密码
var passwordTwo=document.myform.passwordtwo.value;   //第二次输入新密码
if(oldPassword != nowPassword)
{
alert("旧密码不正确");
document.myform.oldPassword.focus();
return false;
}
if(oldPassword.length == 0)
{
alert("旧密码不能为空");
document.myform.guestname.focus();
return false;
}
if(password.length == 0)
{
alert("密码不能为空");
document.myform.password.focus();
return false;
}
if(passwordTwo.length == 0)
{
alert("确认密码不能为空");
document.myform.passwordtwo.focus();
return false;
}
if(password.length != 0 && passwordTwo != password)
{
alert("两次密码不一致");
return false;
}
return true;
}
</script></head>
<body onload="javascript:document.myform.oldpassword.focus()">
<form action="../do/doUpdatePassword.jsp" onsubmit="return check()" name="myform" method="post">
<TABLE class=tableborder cellSpacing=1 cellPadding=4 width="100%">
<TR>
    <TD class=header colSpan=12>修改密码</TD>
</TR>
<TR>
    <TD align="center" class=altbg1>旧&nbsp;密&nbsp;码:</TD>
    <TD class="altbg2"><input type="password" name="oldpassword" style="width:200px" /></TD>
</TR>
<TR>
    <TD align="center" class=altbg1>新&nbsp;密&nbsp;码:</TD>
    <TD class="altbg2"><input type="password" name="password" style="width:200px" /></TD>
</TR>
<TR>
    <TD align="center" class=altbg1>确认密码:</TD>
    <TD class="altbg2"><input type="password" name="passwordtwo" style="width:200px" /></TD>
</TR>
<TR>
     <TD class=altbg1></TD>
     <TD class=altbg2 colSpan=11>
     <INPUT type="submit" name="sub" value=" 提交 "> &nbsp; <INPUT type="reset" name="res" value=" 重置 ">
     </TD>
          </TR>
</TABLE>
</form></body>
</html>
可是执行后check()部分根本没作用,怎么回事?疯了要。

解决方案 »

  1.   


    var nowPassword= "<%=session.getAttribute("loginPwd").toString()%>"; //这里是登陆时记录的密码
      

  2.   

    有个地方少了<%%>var nowPassword=(String)session.getAttribute("loginPwd"); var nowPassword="<%=(String)session.getAttribute("loginPwd")%>"; 
      

  3.   

    var nowPassword = decodeURIComponent("<%=java.net.URLEncoder.encode(session.getAttribute("loginPwd").toString(), "UTF-8").replace('+', ' ')%>"); //这里是登陆时记录的密码为了防止 密码中的特殊字符 破坏 JavaScript, 可以用这个
    不过 HTML 中含有用户的当前密码信息, 这样是不是威胁到用户的安全性
      

  4.   

    OK了是你的JS写错了更正如下<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%><html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gbk">
    <title>修改密码</title>
    <LINK href="../style/adminCss.css" type=text/css rel=stylesheet>
    <script language="javascript" type="text/javascript">
    function check(){    
        var oldPassword=document.myform.oldpassword.value;  //旧密码
       //把它放到页面的<input type="hidden">隐藏为FORM的一个属性
       // var nowPassword=(String)session.getAttribute("loginPwd"); //这里是登陆时记录的密码
        var password=document.myform.password.value;     //新密码
        var passwordTwo=document.myform.passwordtwo.value;   //第二次输入新密码
       // if(oldPassword != nowPassword)
       // {
           // alert("旧密码不正确");
            //document.myform.oldPassword.focus();
           // return false;
      //  }
        if(oldPassword.length == 0)
        {
            alert("旧密码不能为空");
            //guestname没这个属性
            //document.myform.elements["guestname"].focus();
            return false;
        }
        if(password.length == 0)
        {
            alert("密码不能为空");
            document.myform.elements["password"].focus();
            return false;
        }
        if(passwordTwo.length == 0)
        {
            alert("确认密码不能为空");
            document.myform.elements["passwordtwo"].focus();
            return false;
        }
        if(password.length != 0 && passwordTwo != password)
        {
            alert("两次密码不一致");
            return false;
        }
         return true;
    }
    </script> </head>
    <body onload="javascript:document.myform.oldpassword.focus()">
    <form action="../do/doUpdatePassword.jsp" method="post" name="myform"
    onsubmit="return check()" >
    <TABLE class=tableborder cellSpacing=1 cellPadding=4 width="100%">
    <TR>
    <TD class=header colSpan=12>
    修改密码
    </TD>
    </TR>
    <TR>
    <TD align="center" class=altbg1>
    旧&nbsp;密&nbsp;码:
    </TD>
    <TD class="altbg2">
    <input type="password" name="oldpassword" style="width:200px" />
    </TD>
    </TR>
    <TR>
    <TD align="center" class=altbg1>
    新&nbsp;密&nbsp;码:
    </TD>
    <TD class="altbg2">
    <input type="password" name="password" style="width:200px" />
    </TD>
    </TR>
    <TR>
    <TD align="center" class=altbg1>
    确认密码:
    </TD>
    <TD class="altbg2">
    <input type="password" name="passwordtwo" style="width:200px" />
    </TD>
    </TR>
    <TR>
    <TD class=altbg1></TD>
    <TD class=altbg2 colSpan=11>
    <INPUT type="submit" name="sub" value=" 提交 ">
    &nbsp;
    <INPUT type="reset" name="res" value=" 重置 ">
    </TD>
    </TR>
    </TABLE>
    </form> </body>
    </html>