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?
- <div class="row no-gutters" onchange="checkDate()">
- <div class="col-md-2">
- <label for="PTWContent.StartDate" class="form-control badge-primary">2. Start Date</label>
- <input asp-for="PTWContent.StartDate" class="form-control" id="sdate" />
- <span id="msg1" class="text-danger"></span>
- </div>
- <div class="col-md-2">
- <label for="PTWContent.EndDate" class="form-control badge-primary">3. End Date</label>
- <input asp-for="PTWContent.EndDate" class="form-control" id="edate" />
- <span id="msg2" class="text-danger"></span>
- </div>
- </div>
- <div class="col-md-12 d-flex justify-content-center">
- <button id="btnSubmit">Save</button>
- </div>
- <script>
- function checkDate() {
- var msg1 = ""; var msg2 = "";
- var startDate = new Date($('#sdate').val());
- var today = new Date();
- if (startDate.toLocaleDateString() > today.toLocaleDateString() || startDate.toLocaleDateString() == today.toLocaleDateString()) {
- $("#edate").attr("disabled", false);
- var endDate = new Date($('#edate').val());
- if (startDate.toLocaleDateString() < endDate.toLocaleDateString() || startDate.toLocaleDateString() == endDate.toLocaleDateString()) {
- $("#btnSubmit").attr("disabled", false);
- }
- else {
- msg2 = msg2 + "Due Date cannot be before Start Date";
- $("#btnSubmit").attr("disabled", true);
- }
- }
- else {
- msg1 = msg1 + "Start Date cannot be in the past";
- $("#btnSubmit").attr("disabled", true);
- $("#edate").attr("disabled", true);
- }
-
- $("#msg1").text(msg1);
- $("#msg2").text(msg2);
-
- }
- </script>