提交前的表单验证脚本

时间:2015-05-24 20:09:36

标签: javascript forms validation

有一个表单,在提交时会打开新页面,以防验证正常。但是,窗口在每种情况下都会打开,我想验证不起作用,而它应该...... 如果你能指出我正确的方向,我将不胜感激。

<!DOCTYPE html>
<html>

<head>
    <script type="text/javascript">
        function overallcheck() {
            if (!checkjob() || !checkEmail() || !Checkname()) {
                writeTo("problemArea", "Error messages area");
                return false;
            }
            if (!Checkname()) {
                writeTo("problemArea", "Please inser a valid name");
                return false;
            }
            if (!checkEmail()) {
                writeTo("problemArea", "Please inser a valid email");
                return false;
            }
            if (!checkjob()) {
                writeTo("problemArea", "Please inser your job");
                return false;
            }
            elsereturn true;
        }

        function Checkname() {
            clearElement("problemArea");
            var fullname = document.forms['form'].fullname.value;
            if (fullname.length == 0 || !isNaN(fullname))
                return false;
        }

        function checkEmail() {
            clearElement("problemArea");
            var mail = document.forms['form'].Email.value;
            if (mail == '' || mail.indexOf('@') == -1 || mail.indexOf('.') == -1)
                return false;
        }
        }

        function checkjob() {
            clearElement("problemArea");
            var i;
            for (i = 0; i < 4; i++) {
                if (document.forms['form'].job[i].checked) {
                    return true;
                }
            }
            return false;
        }

         //-->
    </script>

</head>

<body>

    <form onsubmit=( "return overallcheck();") action="another.html" id=form target="_blank" method="GET">
        <table>

            <button type="submit" onclick="">submit</button>

    </form>
</body>

</html>

1 个答案:

答案 0 :(得分:0)

嗯......这是无效的JavaScript。正如NewToJS在评论中指出的那样,elsereturn true;不正确。

您需要使用:

else {
    return true;
}

此外,此(onsubmit=( "return overallcheck();"))将无效。你需要这样做:

<form onsubmit="return overallcheck();" action="another.html" id="form" target="_blank" method="GET">