尝试以电子邮件重置页面的形式练习一些表单验证。
这是我的代码:
<!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>
如果电子邮件输入与电子邮件格式不匹配,并且密码输入不相同,我希望弹出警报。然而,当我点击提交时,我什么也没得到。
我正在使用this教程作为指导。我想不知何故我仍然缺少一些东西。
最佳答案
错误:
Timestamp: 8/14/2013 11:19:50 AM Error: TypeError: document.getElementByID is not a function Source File: file:///C:/Users/developer/Desktop/tes.html?user_email=asd%40yahoo.com&new_pwd=asd&confirm_pwd=asd Line: 13
你的问题是函数名称。
document.getElementById
不是 document.getElementByID
您应该获取 HTML 元素的值。
var pass1=document.getElementById("new_pwd").value;
var pass2=document.getElementById("confirm_pwd").value;
关于javascript - 无法让 JavaScript 在提交时运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18222300/