当我的javascript函数返回false时,为什么我的表单仍然会提交?

时间:2012-03-20 16:46:16

标签: javascript html validation

这是我的Javascript formvalidator函数:

function companyName() {
    var companyName = document.forms["SRinfo"]["companyName"].value;
    if (companyName == ""){
    return false;
    } else {
    return true;
    }
}

function companyAdd() {
    var companyAdd1 = document.forms["SRinfo"]["companyAdd1"].value;
    if (companyAdd1 == ""){
    return false;
    } else {
    return true;
    }
}

function companyCity() {
    var companyCity = document.forms["SRinfo"]["companyCity"].value;
    if (companyCity == ""){
    return false;
    } else {
    return true;
    }
}

function companyZip() {
    var companyZip = document.forms["SRinfo"]["companyZip"].value;
    if (companyZip == ""){
    return false;
    } else {
    return true;
    }
}

function enteredByName() {
    var enteredByName = document.forms["SRinfo"]["enteredByName"].value;
    if (enteredByName == ""){
    return false;
    } else {
    return true;
    }
}

function dayPhArea() {
    var dayPhArea = document.forms["SRinfo"]["dayPhArea"].value;
    if (dayPhArea == ""){
    return false;
    }
}

function dayPhPre() {
    var dayPhPre = document.forms["SRinfo"]["dayPhPre"].value;
    if (dayPhPre == ""){
    return false;
    } else {
    return true;
    }
}

function dayPhSub() {
    var dayPhSub = document.forms["SRinfo"]["dayPhSub"].value;
    if (companyAdd1 == ""){
    return false;
    } else {
    return true;
    }
}

function validateForm() {
        if (companyName() && companyAdd() && companyCity() && companyZip() && enteredByName() && dayPhArea() && dayPhPre() && dayPhSub()) {
            return true;        

        } else {
            window.alert("Please make sure that all required fields are completed.");
            document.getElementByID("companyName").className = "reqInvalid";
            companyName.focus();
            return false;
        }
    }

以下是我的所有内容,以防万一与另一个冲突(我使用jquery进行toggle()):

<script type="text/javascript" src="formvalidator.js"></script>
<script type="text/javascript" src="autoTab.js"></script>
<?php 
require_once('mobile_device_detect.php');
include_once('../db/serviceDBconnector.php');
$mobile = mobile_device_detect();

if ($mobile) {
        header("Location: ../mobile/service/index.php");
    if ($_GET['promo']) {
        header("Location: ../mobile/service/index.php?promo=".$_GET['promo']);
    }
}

?>
<script src="http://code.jquery.com/jquery-1.7.1.js"></script>

这是我的表单标签,其函数返回onSubmit:

<form method="POST" action="index.php" name="SRinfo" onsubmit="return validateForm();">

验证工作完美,我测试了所有字段并且我不断获得相应的警报,但是在警报之后表单被提交到mysql并作为电子邮件发送。这是我提交POST数据的代码。

if($_SERVER['REQUEST_METHOD']=='POST') {
// Here I submit to Mysql database and email form submission using php mail()

2 个答案:

答案 0 :(得分:5)

在我看来,这条线很可能会爆炸:

companyName.focus();

我在companyName看到的唯一定义是函数。你不能在函数上调用focus

这会爆发,因此永远无法达到return false

答案 1 :(得分:0)

我会注释掉验证部分中的所有代码,然后返回false。如果这样可以停止发布表单,则执行验证的实际代码中会出现错误。一次添加一个部分,直到找到错误。

我的猜测与詹姆斯建议您关注函数'companyName'的情况相同。上面的这一行似乎是试图从文档中获取具有相同名称的元素,但是你没有将它赋给变量,以便你可以调用焦点。

相关问题