在提交之前检查密码匹配

时间:2012-11-17 10:38:57

标签: javascript html validation jsp

如何在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>

1 个答案:

答案 0 :(得分:2)

尝试将此添加到您的上一个代码:

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

为什么每次都要制作新变量apass