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

标签 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()

最佳答案

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

companyName.focus();

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

这会爆炸,所以永远不会到达return false

关于javascript - 为什么当我的 javascript 函数返回 false 时我的表单仍然提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9791201/

相关文章:

javascript - 我认为我没有正确有效地实现和使用可读流?

javascript - winjs:如何使用 winjs 库导航到 html 页面。?

javascript - 如何在稍后的测试中重用产生的值

html - 修复 IE6 布局错误的策略?

java - try-catch找不到符号问题

javascript - 输入字段上的两个函数调用,一个是 onBlur,一个不在 Angular 中

python - 使用太多运算符验证后缀

javascript - 动态更改 iframe 的 onload

html - 如何始终检测浏览器对 HTML5 搜索输入的支持?

html - 设置宽度 :auto leads to width:100%