Marius Vasile

Marius Vasile

  • 592
  • 1.9k
  • 145.9k

check date with javascript

Apr 9 2021 6:32 AM
I have a javascript to verify dates input and enable a submit button if conditions are met. it is working only if sdate is today. what is wrong?
 
  1. <div class="row no-gutters" onchange="checkDate()">  
  2.     <div class="col-md-2">  
  3.        <label for="PTWContent.StartDate" class="form-control badge-primary">2. Start Date</label>  
  4.        <input asp-for="PTWContent.StartDate" class="form-control" id="sdate" />  
  5.        <span id="msg1" class="text-danger"></span>  
  6.      </div>  
  7.      <div class="col-md-2">  
  8.        <label for="PTWContent.EndDate" class="form-control badge-primary">3. End Date</label>  
  9.        <input asp-for="PTWContent.EndDate" class="form-control" id="edate" />  
  10.        <span id="msg2" class="text-danger"></span>  
  11.      </div>  
  12. </div>  

  1. <div class="col-md-12 d-flex justify-content-center">    
  2.    <button id="btnSubmit">Save</button>    
  3. </div>   
  1. <script>  
  2.    function checkDate() {  
  3.      var msg1 = ""; var msg2 = "";  
  4.      var startDate = new Date($('#sdate').val());  
  5.      var today = new Date();  
  6.    if (startDate.toLocaleDateString() > today.toLocaleDateString() || startDate.toLocaleDateString() == today.toLocaleDateString()) {  
  7.                 $("#edate").attr("disabled"false);  
  8.                 var endDate = new Date($('#edate').val());  
  9.                 if (startDate.toLocaleDateString() < endDate.toLocaleDateString() || startDate.toLocaleDateString() == endDate.toLocaleDateString()) {  
  10.                     $("#btnSubmit").attr("disabled"false);  
  11.                 }  
  12.                 else {  
  13.                     msg2 = msg2 + "Due Date cannot be before Start Date";  
  14.                     $("#btnSubmit").attr("disabled"true);  
  15.                 }  
  16.             }  
  17.             else {  
  18.                 msg1 = msg1 + "Start Date cannot be in the past";  
  19.                 $("#btnSubmit").attr("disabled"true);  
  20.                 $("#edate").attr("disabled"true);  
  21.             }  
  22.   
  23.             $("#msg1").text(msg1);  
  24.             $("#msg2").text(msg2);  
  25.   
  26.         }  
  27.     </script>  
 
 
 
 

Answers (3)