2011-09-20 92 views
0
<form:form method="get" action="" onsubmit="return matchPassword();" id="myform" commandName="users"> 
<ul> 


     <li> 
      <form:label path="password" id="newPwd"><spring:message code="label.password"/></form:label> 
      <form:input path="password"/> 
     </li> 
     <li> 
      <form:label path="password" id="RePwd"><spring:message code="label.password"/></form:label> 
      <form:input path="password"/> 
     </li> 

     <li> 
      <label>&nbsp;</label><input type="submit" class="btn" value="<spring:message code="label.adduser"/>"/> 
     </li> 
    </ul> 
</form:form> 

由於我有兩個密碼輸入框。現在我想比較兩個框的值是否相同使用Java腳本。 如何從這些文本框中獲取值,請建議。如何獲得文本框值

回答

0

就這樣做 - 沒有測試過,但它應該把你放在正確的方向。

var newPwd = document.getElementById('newPwd').value; 
    var RePwd = document.getElementById('RePwd').value; 

    if (newPwd != RePwd) 
    { 
     alert("Passwords Don't match"); 
    } 
+0

沒有什麼所謂的'getElementById'。由於用戶似乎是JS中的新手,請避免提供可能無法使用的代碼。 – Saket

0

您可以使用此:

var txt1 = document.getElementById('newPwd'); 
var firstPwd = txt1.value; 

var txt2 = document.getElementById('RePwd'); 
var rePwd = txt2.value; 

,然後比較這兩個變量firstPwdrePwd平等:

if (firstPwd == rePwd) 
// proceed 
else 
// notify error! 
2
window.matchPassword = function(){ 
    return document.getElementById('newPwd').value == document.getElementById('RePwd').value; 
} 
相關問題