2012-11-17 100 views
0

如何在JavaScript中提交表单之前检查密码是否匹配。我的下面的代码是当我重新输入密码时,它应显示“密码不匹配”,如果它们不相同。我如何添加?提交前检查密码匹配

我的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

尝试增加给你的最后一个代码:

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

,为什么你在做新的变量apass每次?

+0

仍然要将该apass编辑为另一个变量,并且谢谢 – Murali

+1

希望这能起作用 – Abubakkar