在onsubmit上调用两个函数

时间:2016-02-29 19:15:43

标签: javascript

基本上我有三个函数(Format(),checkEmpty()和validDate())。一个确保格式是正确的,第二个是输入的东西,第三个检查日期不是过去等等(基本航空公司网页的预订表格)。我想要运行这两个函数,如果两个函数都有效,那么转到testpage.py

<Form action="testpage.py" method="POST" name="MyForm" onsubmit="return !!(Format() & validDate());">

function checkEmpty(userdate) {


      if (userdate == '' || userdate == null  ) {
            return false; 
      }
      else
      {
            return true; 
      }
}


function Format() { 

       var departuredate = document.getElementById("departdate").value; 
       var arrivaldate = document.getElementById("arrivedate").value;

       var pattern1 = /^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/;  //dd/mm/yyyy

       var pattern2 = /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/;   //yyyy-mm-dd  for Chrome with date type input field

       var output1a = pattern1.test(departuredate);
       var output1b = pattern2.test(departuredate);

       var output2a = pattern1.test(arrivaldate);
       var output2b = pattern2.test(arrivaldate);


       if(!checkEmpty(departuredate))
       {

            alert("Empty date - Please enter date again in format: dd/mm/yyyy");
            document.getElementById("departdate").focus();
            document.getElementById("departdate").style.border='2px solid red';   
            return false;  

       } 
       else if(!checkEmpty(arrivaldate))
       {
            alert("Empty date - Please enter date again in format: dd/mm/yyyy");
            document.getElementById("arrivedate").focus();
            document.getElementById("arrivedate").style.border='2px solid red';
            return false;              
       }
       else
       {

            if (output1a){
                  departdate = departuredate.replace(/(\d{2})\/(\d{2})\/(\d{4})/, "$2/$1/$3");
                  alert('Dates are validated');
                  return true;
            }
            else if (output1b)
            {
                  departdate = departuredate.replace(/(\d{4})\-(\d{2})\-(\d{2})/, "$2/$3/$1"); 
                  alert('Dates are validated');
                  return true;
            }
            else
            {
                  alert("Incorrect date format - Please enter the arrival date again in format: dd/mm/yyyy");
                  document.getElementById("departdate").focus();
                  document.getElementById("departdate").style.border='2px solid red';   
                  return false;

            }         


            if (output2a)
            {

                  arrivedate = arrivaldate.replace(/(\d{2})\/(\d{2})\/(\d{4})/, "$2/$1/$3");
                  alert('Dates are validated');
                  return true;
            }
            else if (output2b)
            {
                  arrivedate = arrivaldate.replace(/(\d{4})\-(\d{2})\-(\d{2})/, "$2/$3/$1")
                      alert('Dates are validated');
                  return true;          
            }

            else
            {
                  alert("Incorrect date format - Please enter the departure date again in format: dd/mm/yyyy");
                  document.getElementById("arrivedate").focus();
                  document.getElementById("arrivedate").style.border='2px solid red';              
                  return false;

            }
      }
} 

function validDate(){

       var months3 = 90 * 24 * 60 * 60 * 1000; //3 months 
       var departuserspecifiedTime = departuredate.getTime();
       var arrivaluserspecifiedTime = arrivaldate.getTime();  
       var currentTime = CurrentDate.getTime(); //current time and date
       var departdifference = departuserspecifiedTime - currentTime; //difference between departure time and the current time
       var arrivaldifference = arrivaluserspecifiedTime - currentTime; //difference between arrival time and the current time

            if (departdifference <= (1000 * 60 * 60)) //if the departure time is in the past or within an hour of the current time, it is invalidated as it is too soon
            {
                document.getElementById("temp").innerHTML = "Date selected is in the past";
                document.MyForm.departdate.focus();
                document.getElementById("departdate").style.border='1px solid red';
                return false;
            }

            if (arrivaldifference <= (1000 * 60 * 60)) //if the arrival time is in the past or within an hour of the current time, it is invalidated as it is too soon
            {
                document.getElementById("temp").innerHTML = "Date selected is in the past";
                document.MyForm.arrivedate.focus();
                document.getElementById("arrivedate").style.border='1px solid red';
                return false;
            }


            if (departdifference && arrivaldifference >= months3) //if the departure/arrival date is over 3 months away from todays date it is invalidated
            {
                document.getElementById("temp").innerHTML = "Only 3 months advance booking is allowed";
                document.getElementById("departdate").style.border='1px solid red';
                document.getElementById("arrivedate").style.border='1px solid red';
                return false;

            }

            if (arrivaluserspecifiedTime < departuserspecifiedTime) // if the arrival date is before the departure date it is invalidated 
            {
                alert("Arrival date selected is in the past") ;
                document.getElementById("departdate").focus();
                document.getElementById("arrivedate").focus();
                    document.getElementById("departdate").style.border='1px solid red';
                    document.getElementById("arrivedate").style.border='1px solid red';
                    return false;

            }

             else 
            {
                // if none of the above situaton's occur then the input is true and validated
                alert('Dates are validated');
                return true;          
            }

}

1 个答案:

答案 0 :(得分:0)

我认为您需要evt.preventDefault() read this,因此您可以避免提交事件。

<Form action="testpage.py" method="POST" name="MyForm" onsubmit="my_validate_function(evt)">

等等:

function validar_campos(evt) {
    var resp;
    resp =  Format();
    if (!resp) {//false
        alert("something goes wrong");
        evt.preventDefault();
    }
    resp =  validDate();
    if (!resp) {
        alert("something goes wrong");
        evt.preventDefault();
    }
}

您也可以使用

<button type="button" onclick="my_validate_function()">Click to submit</button>

而不是提交按钮

相关问题