[英]How to check inequality between two strings in javascript
我有一個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;
}
即使我在新密碼中輸入相同的密碼並確認密碼,但errormatchingpassword仍在jsp中顯示且未提交表單,這里的問題仍然存在。 有人請幫我解決這個問題 ...
我什至嘗試了這個:
if((document.getElementById('newpass')!==(document.getElementById('confirmpass')))){
validationMessage = validationMessage + ' - New password and Confirm password donot match\r\n';
但是問題仍然存在。 需要幫助,在此先感謝...
您正在比較兩個元素,而不是其中包含的值:
if((document.getElementById('newpass').value !==(document.getElementById('confirmpass').value))){
validationMessage = validationMessage + ' - New password and Confirm password donot match\r\n';
}
嘗試這樣的事情
if(document.getElementById('newpass').value !== document.getElementById('confirmpass').value){
validationMessage = validationMessage + ' - New password and Confirm password donot match\r\n';
}
比較元素值而不是元素對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.