1

为客户站点编写验证脚本。每个函数单独正常工作,但是当我从一个函数调用它们时,页面仍然提交。这是我的代码:

    <script type="text/javascript">

    function validateForm() {
      checkDate();
      checkRemainingFields();
      checkPhone();
    }

    /**--------------------------
    //* Validate Date Field script- By JavaScriptKit.com
    //* For this script and 100s more, visit http://www.javascriptkit.com
    //* This notice must stay intact for usage
    ---------------------------**/

    function checkDate(){
      var input=document.forms[0].eventdate;
      var validformat=/^\d{2}\/\d{2}\/\d{4}$/; //Basic check for format validity

      if (!validformat.test(input.value)) {
        alert("Invalid Day, Month, or Year range detected. Proper format is MM/DD/YYYY, please correct and submit again.")
        return false;
      } else{  //Detailed check for valid date ranges
      var monthfield=input.value.split("/")[0];
      var dayfield=input.value.split("/")[1];
      var yearfield=input.value.split("/")[2];
      var dayobj = new Date(yearfield, monthfield-1, dayfield);
    }

    if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield)) {
      alert("Invalid Day, Month, or Year range detected. Proper format is MM/DD/YYYY, please correct and submit again.")
      return false;
      } else return true;
    }

    function checkRemainingFields() {
      var theme=document.forms[0].theme;
      var text=document.forms[0].text;
      var name=document.forms[0].contactperson;

      if (theme.value.length==0) {
        alert("Invalid theme value.  Please correct the theme field to continue.");
        return false;
      } else if (text.value.length==0) {
        alert("Invalid description value. Please correct the desciption field to continue.");
        return false;
      } else if (name.value.length==0) {
        alert("Invalid name value.  Please correct the name field to continue.");
        return false;
      } else return true;
    }

    function checkPhone() {
      var input=document.forms[0].contactphone;
      var validformat=/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
      var ext=document.forms[0].extension;

      if (!validformat.test(input.value)) {
        alert("Invalid phone number detected. The proper format is 555-555-5555. Please correct and submit again.");
        return false;
      } else if (ext.value.length != 3){ //Check extension
        alert("Invalid extension.  Please type your 3 digit extension.");
        return false;
      } else return true;
    }

    </script>

validateForm() 是在页面稍后的表单中通过 onsubmit 调用的。我不确定我是否正确地编写了调用。提交表单时,它会通过所有检查并显示每个检查的警报,但页面继续到 php 处理页面。我需要一点帮助。

4

2 回答 2

2

尝试类似:

function validateForm() {
    return checkDate() && checkRemainingFields() && checkPhone();
}

false当这些函数中的任何一个返回时,您需要返回false,否则返回true

等效但更冗长的方式是:

function validateForm() {
    if (!checkDate()) return false;
    if (!checkRemainingFields()) return false;
    if (!checkPhone()) return false;
    return true;
}
于 2012-05-19T00:18:11.250 回答
0
function validateForm() {
  if(checkDate())
     {
       if(checkRemainingFields())
            {
              checkPhone();
            }
       }
  }
于 2012-05-19T00:16:38.613 回答