如何在javascript中检查两个字符串之间的不等式

时间:2014-03-25 06:37:54

标签: javascript jsp

我有一个jsp表单,如下所示:

<form action="ChangePasswordServlet1" method="post" onsubmit="return matchPassWord();">
<table id= "changepwd">
<tr>
<td>Username: </td>
<td><input type="text" name="username" id="username" value="<%out.println( request.getSession().getAttribute("user") );%>"></td>
</tr>
<tr>
<td>Old Password: </td>
<td><input type="password" name="oldpass" id="oldpass"><br/>
<span id="errormissingoldpass" style="display:none"><font color="red">*Please type in your current password</font></span>
</td>
</tr>
<tr>
<td>New Password: </td>
<td><input type="password" name="newpass" id="newpass"><br/>
<span id="errormissingnewpass" style="display:none"><font color="red">*Please type in your new password</font></span>
</td>
</tr>
<tr>
<td>Confirm Password: </td>
<td><input type="password" name="confirmpass" id="confirmpass"><br/>
<span id="errormissingconfirmpass" style="display:none"><font color="red">*Please cofirm your new password by typing it again</font></span>
<span id="errormatchingpassword" style="display:none"><font color="red">*New PassWord and Confirm PassWord don't match</font></span>
</td>
</tr>
<tr>

<td><button type="submit" name="submit" id="submit">SUBMIT</button></td>
</tr>


</table>
</form>

以及用于在客户端验证表单的javascript函数,如下所示:

function matchPassWord(){
    var valid = true;
    var validationMessage = 'Please correct the following errors:\r\n';


    document.getElementById('errormissingoldpass').style.display='none';
    document.getElementById('errormissingnewpass').style.display='none';
    document.getElementById('errormissingconfirmpass').style.display='none';
    document.getElementById('errormatchingpassword').style.display='none';

    if(document.getElementById('oldpass').value.length==0){
        validationMessage = validationMessage + '  - Please type your current password\r\n';
        document.getElementById('errormissingoldpass').style.display='';
        valid = false;
    }
    else{
        document.getElementById('errormissingoldpass').style.display='none';
        }

    if(document.getElementById('newpass').value.length==0){
        validationMessage = validationMessage + '  - Please type in a new password\r\n';
        document.getElementById('errormissingnewpass').style.display='';
        valid = false;
    }
    else{
        document.getElementById('errormissingnewpass').style.display='none';
    }

    if(document.getElementById('confirmpass').value.length==0){
        validationMessage = validationMessage + '  - Please type again your new password for cofirmation\r\n';
        document.getElementById('errormissingconfirmpass').style.display='';
        valid = false;
    }
    else{
        document.getElementById('errormissingconfirmpass').style.display='none';
    }

    if(!(document.getElementById('newpass')===(document.getElementById('confirmpass')))){
        validationMessage = validationMessage + '  - New password and Confirm password donot match\r\n';
        document.getElementById('errormatchingpassword').style.display='';
        valid = false;
    }

    else{
        document.getElementById('errormatchingpassword').style.display='none';
    }
    if (valid == false){
        alert(validationMessage);
        }
        return valid;

}

这里的问题是即使我在新密码中键入相同的密码并确认密码,错误匹配密码也会在jsp中显示而不提交表单。有人请帮我解决这个问题 ......

我甚至试过这个:

if((document.getElementById('newpass')!==(document.getElementById('confirmpass')))){
            validationMessage = validationMessage + '  - New password and Confirm password donot match\r\n';

但问题仍然存在。需要帮助提前感谢...

2 个答案:

答案 0 :(得分:1)

您要比较两个元素,而不是其中包含的值:

if((document.getElementById('newpass').value !==(document.getElementById('confirmpass').value))){
  validationMessage = validationMessage + '  - New password and Confirm password donot match\r\n';
}

答案 1 :(得分:0)

尝试这样的事情

if(document.getElementById('newpass').value !== document.getElementById('confirmpass').value){
    validationMessage = validationMessage + '  - New password and Confirm password donot match\r\n';
}

比较元素值而不是元素对象。