将输入日期与当前日期进行比较时出现问题

时间:2019-05-12 09:44:12

标签: javascript html validation date datetime

我正在尝试创建一个允许用户输入有关飞机飞行的信息的网页。我想验证航班起飞/到达的日期和时间,以确保到达时间是在起飞之后,而起飞时间是在当前日期之后,等等。 我可以进行出发与到达比较,但是出发与当前日期比较没有任何作用。

对于另一个页面,我在工作的用户输入日期周围添加了Date.parse()(var depDate = Date.parse(document.forms [“ newFlight”] [“ departure_date”]。value;)),但是在这里,这只会使日期比较无效,甚至无法解决当前的日期比较问题。

<head>
<script>
function validateDatePrice() {
  var basePrice = document.forms["newFlight"]["base_price"].value;
  var depTime = document.forms["newFlight"]["departure_time"].value;
  var arrTime = document.forms["newFlight"]["arrival_time"].value;
  var depDate = document.forms["newFlight"]["departure_date"].value;
  var arrDate = document.forms["newFlight"]["arrival_date"].value;
  var currDate = new Date(); 

  if (isNaN(basePrice)) {
    alert("Please enter a numerical value for base price.");
    return false;
  }
  if (currDate <= depDate) {
    alert("The departure date must be after the current date.");
    return false;
  }
  if (depDate > arrDate) {
    alert("Arrival date must be the same as or after the departure date.");
    return false;
  }
  if (depDate == arrDate && depTime >= arrTime) {
    alert("Arrival time must be later than departure time, if your flight takes off and lands on the same day.");
    return false;
  }
}
</script>
</head>
<body>

<form name = "newFlight" action="/airlineStaff/addFlightAuth" onsubmit="return validateDatePrice()" method="POST">
    Departure Date: <input type="date" name = "departure_date" placeholder="departure date" required/> </br>
    Departure Time: <input type="time" name = "departure_time" placeholder="departure time" required/> </br>

    Arrival Date: <input type="date" name = "arrival_date" placeholder="arrival date" required/> </br>
    Arrival Time: <input type="time" name = "arrival_time" placeholder="arrival time" required/> </br>

    $ <input type="text" name = "base_price" placeholder="base price" required/> </br>

    <input type="submit" value = "Submit"/>
    <input type="reset">
    {% if error %}
        <p class="error"><strong>Error:</strong> {{error}}</p>
    {% endif %}
</form>
</body>
</html>

(currDate <= depDate)的if语句永远不会导致错误,永远不会显示警报消息。 我希望弹出警报消息,并且如果出发日期等于或早于当前日期,则迫使用户确定出发日期。

0 个答案:

没有答案