如何在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>
你的JS功能不作爲代碼發佈。重新發布它以使其可以理解 – polin
您的HTML無效。嘗試通過驗證器。 –