简体   繁体   中英

How to check inequality between two strings in javascript

I have a jsp form as shown below:

<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:

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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