0

I would like to disable certain days in a jQuery UI datepicker based on the days entered in one of the input boxes by the user.

If i enter MWF it should only allow me to only select the dates which falls on either "M"onday , "W"ednesday ot "F"riday

I am able to dynamically construct the return value sting which needs to be set in the return statement.

 for (var j=0; j < arr.length; j++)
      mystr = mystr + "day != " + arr[j] + " && ";

      mystr = mystr.substring(0, mystr.length - 3);

This gives me the following text in mystr variable:

day != 3 && day != 4 && day != 6 

which needs to passed on to the return statement like :

​$("#datepicker").datepicker({
    beforeShowDay: function(date) {
        var day = date.getDay();
        return [(mystr)];
    }
})​​​​​;​

This is not working....

I am able to disable the days i want if i explicitly set it in return statement. But i need to dynamically restrict days selection in the datepicker based on days entered in the text box. I can get the days dynamically and also have the dynamic return statement ready. Its not working.

FULL CODE :

            var batchdays = ['M', 'T', 'W', 'H', 'F', 'S'];
            var batchcode= "MWF";
            var batchdaysarray = batchcode.replace(/\d+/g, '').split('');
            var arr = [];
            var arrayLength = batchdaysarray.length;

            for (var i = 0; i < arrayLength; i++) {
                    if ($.inArray(batchdaysarray[i], batchdays) != -1) {
                        arr.push($.inArray(batchdaysarray[i], batchdays)+1);
                    }
            }

            var mystr = '';

            for (var j=0; j < arr.length; j++)
                mystr = mystr + "day != " + arr[j] + " && ";

            mystr = mystr.substring(0, mystr.length - 3);

            alert(mystr);
            console.log(mystr);

            $('#actual_start_dt').datepicker({
                dateFormat: 'dd-mm-yy',
                prevText: '<i class="fa fa-chevron-left"></i>',
                nextText: '<i class="fa fa-chevron-right"></i>',
                beforeShowDay: function(date) {
                    var day = date.getDay();
                    return [(mystr)];
                }
            });

Please help

Sandy505
  • 888
  • 3
  • 15
  • 26
  • Possible duplicate of [Disable specific days of the week on jQuery UI datepicker](https://stackoverflow.com/questions/2968414/disable-specific-days-of-the-week-on-jquery-ui-datepicker) – Andrew Schultz Apr 01 '18 at 09:30

1 Answers1

0

I probably think that you have already resolved this issue on your own. The problem with the code you have provided is,

return [(mystr)];

According to the jquery-ui documentation beforeShowDay is,

A function that takes a date as a parameter and must return an array with:

  1. true/false indicating whether or not this date is selectable

  2. a CSS class name to add to the date's cell or "" for the default presentation

  3. an optional popup tooltip for this date

In your example, you are however passing string,

 for (var j=0; j < arr.length; j++)
    mystr = mystr + "day != " + arr[j] + " && ";

 mystr = mystr.substring(0, mystr.length - 3); 

which doesn't transpose as statement when used in return [(mystr)];

Instead you can use following,

 beforeShowDay: function(date) {
      var day = date.getDay();
      return [($.inArray(day, arr) != -1)];
  }

Please see this JSFiddle Demoenter link description here.

I hope this is what you are looking for.

Runcorn
  • 5,144
  • 5
  • 34
  • 52
  • Yes... I got it... but this only allows me to select Wednesdays and Fridays even if i am trying to allow "MWF" i.e. Monday too should be allowed... I tried with 4 days like MWFS in this case Wed Fri and Sat are enabled but "Monday" is disabled.... Cant figure out why :-( – Sandy505 Apr 02 '18 at 19:46
  • 1
    Eureka ..... :-) beforeShowDay: function(date) { var day = date.getDay(); return [($.inArray(day, arr) !== -1)]; } This was the lifesaver : https://stackoverflow.com/questions/18867599/jquery-inarray-how-to-use-it-right – Sandy505 Apr 02 '18 at 19:49
  • Glad it worked out for you... I have updated the answer with the suggestion you have mention. Thanks. – Runcorn Apr 03 '18 at 04:38