0

これは、対応する必要な変数がすべて宣言されていることを期待して、日付エントリを無効化/検証するために使用しようとしているコードのスニペットを含む私の html コードです。

       <html>
       <head>
       <title> Booking Page </title>

       <script>

       function Booking(){

        var departuredate = document.getElementById("departdate").value; //departure date selected by user
        var arrivaldate = document.getElementById("arrivedate").value; //arrival date selected by user

        departuredate = new Date(departuredate);
        arrivaldate = new Date(arrivaldate); 

        CurrentDate = new Date(); //todays date

                month = '' + (arrivaldate.getMonth() + 1),
                day = '' + arrivaldate.getDate(),
                year = arrivaldate.getFullYear();
                var adate = [day, month, year].join('/');
                alert(adate);

adate は到着日のみです。出発日に合わせて、コードをコピーして調整する予定です。現在、コードはすべてのエントリを無効にしているようで、完全に有効なエントリを検証することはできません。

         var re = /[0-9]{2}\/[0-9]{2}\/[0-9]{4}/;
         if (!adate.match(re))
         {
            document.getElementById("temp").innerHTML = "Incorrect format"
            document.MyForm.arrivedate.focus();
            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;          
           }

           }
           </script>

           </head>
           <body>

           <H1> Booking Form </H1>

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


            <p>Departure Date:</p>
            <input type=date name="departdate" id="departdate" > 

            <p>Arrival Date:</p>
            <input type=date name="arrivedate" id="arrivedate">

            <input type=submit value="Find flights">

           </Form>        
           </body>
           </html>
4

1 に答える 1