0

I have html form where user enter a date.

I need script what recognize if entered date is equal to some date from array ("takeOverArray["tz27092015", "tz21102015", "tz26092015"]") (that's working) but..

Every array value (["tz27092015", "tz21102015", "tz26092015"]) is array too (datetimes).

So full array is: takeOverArray["tz27092015["mor","lun"]", "tz21102015["lun"]", "tz26092015["eve"]"]

Now, I need when entered date from form is equal to (for example:) "tz27092015" next script will find if array "tz27092015" has specific words: "mor", "lun" or "eve". (that's not working :( and that's my problem )

My Code:

function checkTimeFunction() {
 var iddate= document.getElementById("takeover_date").value; //takeover_date(input from html form)
 var checkTime = "tz" + iddate.split('.').join('');

 if(takeOverArray.indexOf(checkTime, 0) !== -1) //That's working  {
    console.log("date found");

    if (checkTime.indexOf("mor", 0) !== -1) //That's NOT working {

        console.log("morning disabled");

    };
 }
})

So my question is, why

if (checkTime.indexOf("mor", 0) == 0)

not working like previous "indexOf" above this. And how I can figure this problem.

Thanks for your help.

  • `iddate.split('.').join('')` is more efficient as `iddate.replace(/\./g,'')`. Also, *indexOf* starts at 0 by default so no need for the second parameter. – RobG Sep 23 '15 at 03:47

1 Answers1

0

So takeOverArray is an array with strings for values:

var takeOverArray = ["tz27092015['mor','lun']",
                     "tz21102015['lun']",
                     "tz26092015['eve']"];

So if the user selects, say, 27 September, 2015 then the value of iddate will be:

var iddate = '27.09.2015';

Convert that a the format compatible with the values in takeOverArray using replace:

var checkTime = 'tz' + iddate.replace(/\./g,'');

Now you can't use indexOf because it uses strict equality with array values, whereas you want to match checkTime with only part of the value. For that you can use Array.prototype.findIndex. That is an ECMAScript 2015 feature, but you can find a polyfill on MDN.

So first you want to find the index of the matching element:

var index = takeOverArray.findIndex(function(v) {
  return checkTime.indexOf(v) != -1;
})

So now if a suitable value was found, index will be the index of that value, or -1 if none was found. So now:

if (index != -1) {
  console.log("date found");

  // Test if the value at that index contains 'mor'
  if (takeOverArray[index].indexOf('mor') != -1) {
    console.log("morning disabled");
  }
} else {
  console.log("date not found");
}       

And you're done. Here it is as runable code (requires support for findIndex):

// Array.prototype.findIndex polyfill from MDN
if (!Array.prototype.findIndex) {
  Array.prototype.findIndex = function(predicate) {
    if (this === null) {
      throw new TypeError('Array.prototype.findIndex called on null or undefined');
    }
    if (typeof predicate !== 'function') {
      throw new TypeError('predicate must be a function');
    }
    var list = Object(this);
    var length = list.length >>> 0;
    var thisArg = arguments[1];
    var value;

    for (var i = 0; i < length; i++) {
      value = list[i];
      if (predicate.call(thisArg, value, i, list)) {
        return i;
      }
    }
    return -1;
  };
}



    var takeOverArray = ["tz27092015['mor','lun']",
                         "tz21102015['lun']",
                         "tz26092015['eve']"];

    var iddate = '27.09.2015';
    var checkTime = 'tz' + iddate.replace(/\./g,'');
    var index = takeOverArray.findIndex(function(v) {
      return v.indexOf(checkTime) != -1;
    })
    
    if (index != -1) {
      document.write("date found");

      // Test if the value at that index contains 'mor'
      if (takeOverArray[index].indexOf('mor') != -1) {
        document.write("morning disabled");
      }
    } else {
      document.write(checkTime + " not found");
    } 
RobG
  • 142,382
  • 31
  • 172
  • 209
  • Thanks RobG. Unfortunately your solution don't working too :( It's like checkTime[idx] just not transcribed to "tz27... " because, when i write to console "tz27092015.indexOf('mor') == 0" return: true. – DohnnyJepp13 Sep 23 '15 at 04:18
  • It makes no sense to have a change listener on a text input that is readonly. It can't change, so the listener will never be called. – RobG Sep 23 '15 at 05:38
  • I have datepicker (https://jqueryui.com/datepicker/ ) in input and i don't want let user writing to input something else, user just gonna pick a date. Var iddate (document.getElementById("takeover_date").value;) works perfectly, so "readonly" is ok. .. And i just tried to remove it and it works same like version with readonly. – DohnnyJepp13 Sep 23 '15 at 12:00
  • Yes, i wrote it bad.. It's like your second code: var takeOverArray = ["tz27092015['mor','lun']", "tz21102015['lun']", "tz26092015['eve']"]; – DohnnyJepp13 Sep 23 '15 at 12:14