2012-11-17 6 views
0

Javascript에서 양식 제출 전에 일치하는 비밀번호를 확인하는 방법. 내 아래의 코드는 암호를 다시 입력 할 때 동일하지 않으면 "일치하지 않는 암호"가 표시되어야합니다. 어떻게 추가 할 수 있습니까?제출하기 전에 비밀번호를 확인하십시오.

if(apass=="") { 
alert("Confirm password!"); 
return false; 
} else if(apass != document.reg.pwd.value){ 
alert("Confirm Password doesn't match"); 
return false; 
} 

을하고 왜 새로운 변수 apass마다하고 있습니다 :

내 JSP 페이지

<body bgcolor="#33CCCC"> 
<form name="reg" action="Register" method="post" onsubmit="return validate()"> 
    <table cellpadding=4 cellspacing=2 border=1 align="center"> 
     <tr> 
      <th bgcolor="#999999" colspan=2> 
       <b> 
        First Name<sup> 
        *</sup> 
       </b> 
       <br> 
       <input type="text" name="firstname" value="" size=15 maxlength=20> 
       </td> 
       <td valign=top> 
        <br> 
        <input type="text" name="surname" value="" size=15 maxlength=20> 
       </td> 
     </tr> 
     <tr bgcolor="#999966"> 
      <td valign=top> 
       <b> 
        E-Mail<sup> 
        *</sup> 
       </b> 
       <br> 
       <input type="text" name="email" value="" size=25 maxlength=125> 
       <br> 
      </td> 
      <td valign=top> 
       <b> 
        Zip Code<sup> 
        *</sup> 
       </b> 
       <br> 
       <input type="text" name="zipcode" value="" size=5 maxlength=6> 
      </td> 
     </tr> 
     <tr bgcolor="#999966"> 
      <td valign=top colspan=2> 
       <b> 
        User Name<sup> 
        *</sup> 
       </b> 
       <br> 
       <input type="text" name="uid" size=10 value="e" maxlength=10> 
      </td> 
     </tr> 
     <tr bgcolor="#999966"> 
      <td valign=top> 
       <b> 
        Password<sup> 
        *</sup> 
       </b> 
       <br> 
       <input type="password" name="pwd" size=10 value="" maxlength=10> 
      </td> 
      <td valign=top> 
       <b> 
        Confirm Password<sup> 
        *</sup> 
       </b> 
       <br> 
       <input type="password" name="cpwd " size=10 value="" maxlength=10> 
      </td> 
     </tr> 
     <tr bgcolor="#999966"> 
      <td valign=top colspan=2> 
       <b> 
        Town:<sup> 
        *</sup> 
       </b> 
       <br> 
       <input type="text" name="town" size=10 value="" maxlength=10> 
       <br> 
      </td> 
     </tr> 
     <tr bgcolor="#999966"> 
      <td valign=top colspan=2> 
       <b> 
        City:<sup> 
        *</sup> 
       </b> 
       <br> 
       <input type="text" name="city" size=10 value="" maxlength=10> 
       <br> 
      </td> 
     </tr> 
     <tr bgcolor="#663300"> 
      <td align=center colspan=2> 
       <hr> 
       <input type="submit" value="Submit"> 
      </td> 
     </tr> 
    </table> 
</form> 
</body> 

내 JS

<script> 
    function validate() { 
     var auser = document.reg.firstname.value; 
     var invalid = /\W/; 
     //Alphanumeric characters and Underscore permitted 
     if (auser == "") { 
      alert("Enter First name!"); 
      return false; 
     } 
     var aname = document.reg.surname.value; 
     invalid = /[\W_]/; 
     //Alphabets and digits only allowed 
     if (apass == "") { 
      alert("Enter Second name!"); 
      return false; 
     } 
     var apass = document.reg.pwd.value; 
     invalid = /[\W_]/; 
     //Alphabets and digits only allowed 
     if (apass == "") { 
      alert("Enter password!"); 
      return false; 
     } 
     var apassnew = document.reg.cpwd.value; 
     invalid = /[\W_]/; 
     //Alphabets and digits only allowed 
     if (apass == "") { 
      alert("Confirm password!"); 
      return false; 
     } 
    } 
</script> 
+0

JS 기능이 코드로 게시되지 않았습니다. 이해하기 쉽도록 다시 게시하십시오. – polin

+0

귀하의 HTML은 유효하지 않습니다. 검사기를 통과 해보십시오. –

답변

2

시도는 마지막 코드에 이것을 추가?

+0

다른 변수에 대한 apass를 편집하고 감사합니다. – Murali

+1

희망이 작동합니다. – Abubakkar

관련 문제