2016-02-29 133 views
0

基本上我有三个函数(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;   
      } 

} 
+0

这是一些验证?这不可能是*“最小,完整和可验证的例子”*?另外,问题是什么? – adeneo

回答

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> 

而不是提交按钮