2012-03-20 74 views
1

这里是我的Javascript formvalidator功能:为什么我的表单仍然提交时,我的JavaScript函数返回false?

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的为他们的切换()):

<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();"> 

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

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

你也应该验证表单服务器端 – mrtsherman 2012-03-20 16:49:57

回答

5

这似乎对我来说,此行很可能吹起来:

companyName.focus(); 

我看到companyName唯一定义的功能。功能上不能拨打focus

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

+0

良好的调用,该行是从我以前版本的formValidator,这是第一个版本确实工作,但我的老板不喜欢多个警报的遗留下来。当我删除该行时,它不起作用,但我删除了两个'document.getElementByID(“companyName”).className =“reqInvalid”;'&'companyName.focus();'它完美地工作。谢谢! – jnthnjns 2012-03-20 16:57:08

0

我会注释掉验证部分中的所有代码,并简单地返回false。如果这阻止了发布的表单,那么在执行验证的实际代码中存在错误。一次添加一个部件,直到找到错误。

我的猜测与James建议您将焦点放在函数'companyName'上相同。上面的代码似乎试图从文档中获取具有相同名称的元素,但您并未将其分配给变量,以便您可以将焦点放在该变量上。

相关问题