1

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

1 回答 1

2

尝试将此添加到您的最后一个代码中:

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

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

于 2012-11-17T10:50:04.990 回答