尝试以电子邮件重置页面的形式进行一些表单验证。
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title>
form validation
</title>
<script>
function validateForm()
{
var email=document.forms["email_reset"]["user_email"].value;
var atpos=email.indexOf("@");
var dotpos=email.indexOf(".");
var pass1=document.getElementByID("new_pwd");
var pass2=document.getElementByID("confirm_pwd");
if(atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length) {
alert("Your email address is invalid!");
return false;
}
if(pass1 != pass2) {
alert("Your passwords do not match!");
return false;
}
else {
alert("Your password has been successfully reset!");
}
}
</script>
</head>
<body>
<form name="email_reset" onsubmit="validateForm()">
<br />
<font id="title" face="arial" color="white" size="2">[ Enter your email and new password below ]</font><br /><br />
<label id="email_lab" color="white">Email Address:</label>
<input type="text" class="input" name="user_email" id="user_email" maxlength="100" align="right" /><br />
<label id="newpass_lab" color="white">New Password:</label>
<input type="password" class="input" name="new_pwd" id="new_pwd" maxlength="64" align="right" /><br />
<label id="confirmpass_lab" color="white">Confirm Password:</label>
<input type="password" class="input" name="confirm_pwd" id="confirm_pwd" maxlength="64" align="right" /><br />
<input style="width: 25%; margin-bottom: 10px;" type="submit" class="input" id="sub_button" value="Submit" /><br />
</form>
</body>
</html>
如果电子邮件输入与电子邮件格式不匹配,并且密码输入不完全相同,我希望弹出警报。但是,当我单击提交时,我什么也得不到。
我使用本教程作为指南。我想不知何故我仍然错过了一些东西。