简体   繁体   English

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

[英]How to check inequality between two strings in javascript

I have a jsp form as shown below: 我有一个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>

and a javascript function for validating the form at client side as shown below: 以及一个用于在客户端验证表单的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;

}

The problem here is even though i type in same password in new password and confirm password the errormatchingpassword is getting displayed in jsp and not submitting the form. 即使我在新密码中输入相同的密码并确认密码,但errormatchingpassword仍在jsp中显示且未提交表单,这里的问题仍然存在。 Someone please help me solving this ... 有人请帮我解决这个问题 ...

I tried even this: 我什至尝试了这个:

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

But still the problem persists. 但是问题仍然存在。 Help needed thanks in advance... 需要帮助,在此先感谢...

You're comparing the two elements, not the values contained therein: 您正在比较两个元素,而不是其中包含的值:

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

try something like this 尝试这样的事情

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

Compare element value instead of element object. 比较元素值而不是元素对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM