0

I am trying to validate my form and this is my code. It does work, but I need to click it twice for it to be correct. The first click is an error. Does anyone know why? Thanks in advance!

$.validator.addMethod('checkStartDate', function(value, element) {
  checkStartDate(value);
  return $('#validateDate').val();
});

function checkStartDate(value) {
  $.ajax({
    method: 'GET',
    url: '/API/AccountTimeTable/' + getIdFromAddressBar() + '?pgNo=1&pgSize=999'
  }).done(function(data) {
    console.log(data);
    $.ajax({
      method: 'GET',
      url: '/API/AccountRates/' + data[0].custAccountId
    }).done(function(data) {
      console.log(data);
      var startDataDate = new Date(parseDate(moment(data[0].effectiveStartDate).format('DD/MM/YYYY')));
      var startDate = new Date(parseDate(value));
      console.log(startDataDate);
      console.log(startDate);
      $('#validateDate').val(startDate >= startDataDate);
    });
  });
}
Sparky
  • 98,165
  • 25
  • 199
  • 285
Wolfeatspotatoes
  • 115
  • 1
  • 2
  • 10
  • It's because you're making an AJAX request and the result is asynchronous. jQuery validate has built in ways to do what you require, namely the `remote` property on each validation entity. See the duplicate for more information, specifically this answer as the top one is outdated: https://stackoverflow.com/a/13203690/519413 – Rory McCrossan Aug 06 '19 at 16:29
  • See also the library docs: https://jqueryvalidation.org/remote-method/ – Rory McCrossan Aug 06 '19 at 16:30
  • will look into this thank you! – Wolfeatspotatoes Aug 06 '19 at 16:33

0 Answers0