1

クライアント サイトの検証スクリプトに取り組んでいます。各関数は個別に正しく動作しますが、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 に答える