I have written condition on checking date and if lead time is less than 4 hours, show error message and do not allow further action to happen. In Android mobile and website it is working fine.

In Iphone safari browser it doesnt show my message and doesnot return false and continues to perform next action. What is wrong with my code

function validate_form() {
  var mytext = "";
  document.getElementById("mytext").innerHTML = "";

  if ($("#pickup_time").val().trim() !== "") {
    var inputDate = new Date($("#pickup_time").val().trim());
    var todaysDate = new Date();

    if (inputDate.setHours(0, 0, 0, 0) == todaysDate.setHours(0, 0, 0, 0)) {
      var pickup_time = $("#pickup_time").val().trim();
      var pickup_min = pickup_time.substr(pickup_time.length - 2);
      pickup_time = pickup_time.substr(pickup_time.length - 5);
      pickup_time = pickup_time.substr(0, 2);
      var date = new Date();
      var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
      var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
      hours = hours + 4;

      if (pickup_time === hours) {
        if (pickup_min >= minutes) {
          return true;
        } else {
          document.getElementById("mytext").innerHTML = "Minimum lead time required is 4Hrs to schedule a Car!";
          return false;
        }
      } else if (pickup_time < hours) {
        document.getElementById("mytext").innerHTML = "Minimum lead time required is 4Hrs to schedule a Car!";
        return false;
      }
    }
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="success.html" method="post" class="chafviews" id="booknow" onsubmit="return validate_form(event)">
  <div class="pickCont">
    <input type="text" class="textField pick_datepicker" value="" name="pickup_time" id="pickup_time" placeholder="Select start date" />
    <div class="fieldLable">Pickup Date and Time</div>
  </div>
  <div class="pickCont">
    <input type="text" class="textField drop_datepicker" value="" name="dropoff_time" id="dropoff_time" placeholder="Select end date" />
    <div class="fieldLable">Drop-off Date and Time</div>
  </div>
  <p id="mytext" style="color: red; margin-top: 50px; font-size: 13px;"></p>
  <input type="submit" class="selfBook" value="Proceed" />
</form>

0 Answers