我正在尝试使用javascript验证密码字段 . 确保用户在确认密码字段中重新输入密码时,它应该是相同的 .
这是我试图做的 . 这是表格 . onsubmit =“validate_reg()”是使用javascript进行的另一个验证,以确保必须填写所有字段 .
<form name="register" action="signup.php" onsubmit="return validate_reg()"enctype="multipart/form-data" method="post" >
<table width="600" border="0">
<tr><td width="210" height="45">Username*:</td><td>
<input type="text" size="40" name="userUsername" id="user_username" /></td></tr>
<tr><td width="210" height="45">Password*:</td><td>
<input type="password" size="40" name="userPassword" id="user_password"/></td></tr>
<tr><td width="210" height="45">Re-type Password:</td><td>
<input type="password" size="40" name="userPasswordConfirm"
id="user_password_confirm"/></td></tr>
</table>
</form>
这是javascript代码:
<!--================Javascript for the validation of the Password Confirmation==========================-->
<script type="text/javascript" language="javascript">
function validatepw() {
if ( document.register.user_password.value != document.register.user_password_confirm.value)
{
alert('Passwords did not match!');
return false;
}else{
document.register.submit();
return true;
}
}
</script>
<!--================Javascript for the validation of the required fields ================================-->
<script type="text/javascript">
function validate_reg()
{
var isValid = true;
// using OLD method of using name to find the control
if ( document.register.user_username.value == "")
{
document.register.user_username.style.backgroundColor="red";
isValid=false;
}
else{
document.register.user_username.style.backgroundColor="white";
}
if ( document.register.user_password.value == "")
{
document.register.user_password.style.backgroundColor="red";
isValid=false;
}
else{
document.register.user_password.style.backgroundColor="white";
}
if ( document.register.user_password_confirm.value == "")
{
document.register.user_password_confirm.style.backgroundColor="red";
isValid=false;
}
else{
document.register.user_password_confirm.style.backgroundColor="white";
}
}
</script>
1 回答
你在哪里打电话给validatepw?
可以在两个pwd字段中添加onchange =“javascript:validatepw()”