javascript - Call two functions when onsubmit -


basically have 3 functions (format(), checkempty() , validdate()). 1 makes sure format correct second has been entered , third checks dates not in past etc. (booking form basic airline webpage). want both functions run , if both valid move 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  chrome 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 validated');                   return true;             }             else if (output1b)             {                   departdate = departuredate.replace(/(\d{4})\-(\d{2})\-(\d{2})/, "$2/$3/$1");                    alert('dates validated');                   return true;             }             else             {                   alert("incorrect date format - please enter 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 validated');                   return true;             }             else if (output2b)             {                   arrivedate = arrivaldate.replace(/(\d{4})\-(\d{2})\-(\d{2})/, "$2/$3/$1")                       alert('dates validated');                   return true;                       }              else             {                   alert("incorrect date format - please enter 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 , date        var departdifference = departuserspecifiedtime - currenttime; //difference between departure time , current time        var arrivaldifference = arrivaluserspecifiedtime - currenttime; //difference between arrival time , current time              if (departdifference <= (1000 * 60 * 60)) //if departure time in past or within hour of current time, invalidated             {                 document.getelementbyid("temp").innerhtml = "date selected in past";                 document.myform.departdate.focus();                 document.getelementbyid("departdate").style.border='1px solid red';                 return false;             }              if (arrivaldifference <= (1000 * 60 * 60)) //if arrival time in past or within hour of current time, invalidated             {                 document.getelementbyid("temp").innerhtml = "date selected in past";                 document.myform.arrivedate.focus();                 document.getelementbyid("arrivedate").style.border='1px solid red';                 return false;             }               if (departdifference && arrivaldifference >= months3) //if departure/arrival date on 3 months away todays date invalidated             {                 document.getelementbyid("temp").innerhtml = "only 3 months advance booking allowed";                 document.getelementbyid("departdate").style.border='1px solid red';                 document.getelementbyid("arrivedate").style.border='1px solid red';                 return false;              }              if (arrivaluserspecifiedtime < departuserspecifiedtime) // if arrival date before departure date invalidated              {                 alert("arrival date selected in 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 above situaton's occur input true , validated                 alert('dates validated');                 return true;                       }  } 

i think need evt.preventdefault() read this, can avoid submit event.

<form action="testpage.py" method="post" name="myform" onsubmit="my_validate_function(evt)"> 

and this:

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();     } } 

you use

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

instead of submit button


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -