1

JQuery noob here. I need some help it's prob basic, but I would really appreciate if someone would help me. I am working on an old grotesque page that needs to compare dates for departure/arrival to see if they are invalid. This is the logic I came up with to compare the dates which works perfectly (for one date).

The issue I have though is there are 9 dates that need to be compared, #AD1 and #DD1 through #AD9 and #DD9, with each line in a separate table row with a class named ".dateVal". How would I encapsulate this code to run through these 9 dates in a function? I really appreciate the help in advance.. Thanks MUCH!

  var strErrors = '';
  var strRequired = '';

  var iniString = '';
  var endString = '';
  var dateIni = $('#AD1').val().split('/');
  var dateEnd = $('#DD1').val().split('/');
  $.each(dateIni, function (index, value) {
   iniString = iniString + value;
  })
  $.each(dateEnd, function (index, value) {
   endString = endString + value;
  })

  iniString = parseInt(iniString, 10);
  endString = parseInt(endString, 10);


  if (iniString > endString) {
   strErrors = strErrors + ' - Departure/Arrival dates are incorrect.  \n'
  }
marcus314
  • 41
  • 1
  • 5
  • Have you seen this: http://stackoverflow.com/questions/1353684/detecting-an-invalid-date-date-instance-in-javascript ? – Waleed Khan Aug 22 '12 at 16:10
  • The date's are in a goofy format (not in standard date format). My logic works fine I tested it, and it's perfect. I just want to wrap up my logic into a function that runs through the 9 table rows.. Thanks for the input though! – marcus314 Aug 22 '12 at 16:12

2 Answers2

0

One way would by to use concatenation in the jQuery selectors and a simple JavaScript for-loop:

var strErrors = '';

var iniString = '';
var endString = '';

for (var ii = 1; ii <= 9; ii++) {
    var dateIni = $('#AD' + ii).val().split('/');
    var dateEnd = $('#DD' + ii).val().split('/');

    $.each(dateIni, function (index, value) {
        iniString = iniString + value;
    });

    $.each(dateEnd, function (index, value) {
        endString = endString + value;
    });

    iniString = parseInt(iniString, 10);
    endString = parseInt(endString, 10);   

    if (iniString > endString) {
        strErrors = strErrors + ' - Departure/Arrival dates are incorrect.  \n'
    }
}

Now, if you wanted to encapsulate that logic into a couple of functions:

var strErrors = '';

function checkAllDates() {
    for (var ii = 1; ii <= 9; ii++) {
        checkDate(ii);
    }
}

function checkDate(ii) {
    var iniString = '';
    var endString = '';

    var dateIni = $('#AD' + ii).val().split('/');
    var dateEnd = $('#DD' + ii).val().split('/');

    $.each(dateIni, function (index, value) {
        iniString = iniString + value;
    });

    $.each(dateEnd, function (index, value) {
        endString = endString + value;
    });

    iniString = parseInt(iniString, 10);
    endString = parseInt(endString, 10);   

    if (iniString > endString) {
        strErrors = strErrors + ' - Departure/Arrival dates are incorrect.  \n'
    }
}
FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
  • Thanks for the advice. I'm going to give this a shot.. I will let you know how it works! – marcus314 Aug 22 '12 at 16:18
  • Thanks again for your help. How would I check if each #AD or #AD have a value? The #AD and #DD are coming from an html select. The user has an option to choose between 1-9 days. Do you follow what I'm trying to convey lol? – marcus314 Aug 22 '12 at 16:28
  • Do you have a no-value option? If you just have 1 through 9 in a select, one of them will be selected. – FishBasketGordo Aug 22 '12 at 17:31
  • I figured it out. Thanks again for your help. I truly appreciate it! – marcus314 Aug 22 '12 at 18:09
0

You can avoid the use of $.each by using replace instead of split to get rid of the / characters.

var strErrors = '',
    iniNum,
    endNum,
    dateIni,
    dateEnd,
    numDates = $('.dateVal').length / 2; // assumes matching pairs of dates

// assuming your date values are always yyyy/mm/dd format?
for (var i = 1; i <= numDates; i++) {
    dateIni = $('#AD' + i).val().replace('/','');
    dateEnd = $('#DD' + i).val().replace('/','');

    iniNum = parseInt(iniString, 10);
    endNum = parseInt(endString, 10);   

    if (iniNum > endNum) {
        strErrors += ' - Departure/Arrival dates are incorrect.  \n';
    }
}
jackwanders
  • 15,612
  • 3
  • 40
  • 40