javascript电子邮件验证无效

时间:2013-01-21 06:51:42

标签: javascript html

我正在使用javascript验证验证我的表单,因为整个验证工作正常,但电子邮件验证无效。我的表单代码如下

<form method="post" action="contact.php" name="myForm" onsubmit="return validateForm()">

<h3>To know more contact us today.</h3>

    <table>
        <tr>
            <td>Name:
                <br />
                <input id="name" name="name" type="text" />
            </td>
        </tr>
        <tr>
            <td>Contact No:
                <br />
                <input id="contact" name="contact" type="text" />
            </td>
        </tr>
        <tr>
            <td>Email:
                <br />
                <input id="email" type="text" name="email" />
            </td>
        </tr>
        <tr>
            <td>Address:
                <br />
                <textarea cols="34" id="address" name="address" type="text"></textarea>
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" value="Submit" />
            </td>
        </tr>
    </table>
</form>

我的javascript代码如下

  <script type="text/javascript">
        function validateEmail() {

            var emailID = document.["myForm"]["email"].value;
            atpos = emailID.indexOf("@");
            dotpos = emailID.lastIndexOf(".");
            if (atpos < 1 || (dotpos - atpos < 2)) {
                alert("Please enter correct email ID")
                document.myForm.email.focus();
                return false;
            }
            return (true);
        }
        function validateForm() {
            var x = document.forms["myForm"]["name"].value;
            if (x == null || x == "") {
                alert("First name must be filled");
                return false;
            }
            var x = document.forms["myForm"]["contact"].value;
            if (x == null || x == "" || isNaN(document.myForm.contact.value) || document.myForm.contact.value.length != 10) {
                alert("Contact Number Must be 10 Digits");
                return false;
            }
            var x = document.forms["myForm"]["email"].value;
            if (x == null || x == "") {
                alert("Email is must");
                return false;
            }
            else {

                var ret = validateEmail();
                if (ret == false) {
                    return false;
                }
            }
            var x = document.forms["myForm"]["address"].value;
            if (x == null || x == "") {
                alert("Address cannot be empty");
                return false;
            }
            return (true);
        }

    </script>

1 个答案:

答案 0 :(得分:4)

var emailID = document.["myForm"]["email"].value;
                      ^^

语法错误。点符号或方括号表示法。只挑选一个(每个属性)。

我很惊讶Firebug / Chrome开发者工具/ Dragonfly /等在你测试时没有给你一个清晰的指针。