javascript - 无法让 JavaScript 在提交时运行

标签 javascript html validation event-handling

尝试以电子邮件重置页面的形式练习一些表单验证。

这是我的代码:

<!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/

相关文章:

javascript - 如何在JavaScript中更改背景颜色?

javascript - 如何将 TaskT 与 Trampoline 的 monad 实例结合起来进行无堆栈异步计算?

html - 无法添加 100% 高度的 div,并保持现有内容垂直对齐

javascript - 将 CSS 添加到 Iframe 中的 DIV

ruby-on-rails - 验证嵌套集中的节点移动

javascript - jQuery 验证插件不验证 iframe 中的表单

javascript - HTML5 - 跨浏览器 iframe postMessage - child 到 parent ?

javascript - 正则表达式懒惰重复

css - <form> 元素的 <table> 和 <h1> 之间的空格/边距

检查输入是否为数字