Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 67497

End Date greater than Start Date Validation

$
0
0

I am using this code for the validation, it works fine when using the default datepicker in html but when typing the date manually, it's not properly working. For example, when I type a start date then proceed to the endate, the year in the end date automatically reads only the first number of "2019" so you can't finish typing properly since it alerts and can't compare properly.

<input type="date" id="StartDate" />
<input type="date" id="EndDate" />
<script>
console.clear();
 var startDate = document.getElementById("StartDate").value;
 var endDate = document.getElementById("EndDate").value;

function compareDates() {
  if ((Date.parse(endDate) <= Date.parse(startDate))) {
    alert("End date should be greater than Start date");
    document.getElementById("EndDate").value = "";
   }
 }

startDate.addEventListener('input', compareDates);
endDate.addEventListener('input', compareDates);
</script>

Any tips?


Viewing all articles
Browse latest Browse all 67497

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>