Javascript时髦功能

时间:2012-05-19 00:13:16

标签: javascript forms validation

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

    <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>
使用页面稍后的表单中的onsubmit调用

validateForm()。我不确定我是否正确地写了这些电话。提交表单时,它会通过所有检查并显示每个检查的警报,但页面将继续到php处理页面。我需要一些帮助。

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

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