You are viewing limited content. For full access, please sign in.

Discussion

Discussion

Restricting Date Picker

posted on May 22 Show version history

I am trying to restrict the Reporting Pay Period Start Date(.PPstartrange) range to four weeks prior to the Current Pay Period Start Date(.currentPPstart).  I also want the user to only be able to select Sundays.  I would like the Reporting Pay Pay Period End Date(.PPendrange) to automatically be the Saturday two weeks after the Reporting Pay Period Start Date. 

 

I am using a formula in the date field to calculate the Current Pay Period Start and End Dates.

 

I am pretty new to JavaScript and have not figured out the date picker yet.  Could anyone that is familiar with JavaScript help me with a code to make this happen?  Any help is greatly appreciated! smiley

 

0 0
replied on May 26

Using your code Steve Knowlton and some code I got from another post I am close to achieving what I want.  However, I want to add two weeks to the minimum date.  For example, in the image above it shows only the Sundays during the current pay period.  I would like it to also show the two Sundays before the current pay period (i.e. May 10th and 17th).  Below is the code I am currently using.  Can someone tell me how to add 14 days to the code to make the two Sundays prior be enabled?  Any help is greatly appreciated.

Current Pay Period Start Date (.periodBegin)

Current Pay Period End Date (.periodEnd)

Reporting Pay Period Start Date (.dateRange)

$(document).ready(function(){

  function updateMinMaxDates () {
    var minimum = $('.periodBegin input').val(); // get Pay Period Start Date
    var maximum = $('.periodEnd input').val();   // get Pay Period End Date
    var minSplit = [];
    minSplit = minimum.split("/");
    var newMin = (minSplit[2]+"-"+minSplit[0]+"-"+minSplit[1]); 
    var maxSplit = [];
    maxSplit = maximum.split("/");
    var newMax = (maxSplit[2]+"-"+maxSplit[0]+"-"+maxSplit[1]);
    $('.dateRange input').attr('min',newMin); // disable dates before period start date
    $('.dateRange input').attr('max',newMax); // disable dates after period end date
  }
  $('.periodBegin input').change(updateMinMaxDates);
  
    // reset enabled date range when a row is added or removed
  $('.dateRange').on('click',function(){
    updateMinMaxDates();
  });
 
  function isCorrectDate(n) {
    var t = n.getDay();
    var d = n.getDate();
    return (t==0);
  }
  function checkDate(n) {
    return[isCorrectDate(n),""];
  }
  function checkField(input, inst) {
    if ($(input).closest('li').hasClass('dateRange')) {
      $(input).datepicker("option", {beforeShowDay: checkDate});
    }
  }
  $.datepicker.setDefaults( {beforeShow: checkField} );
  window.Parsley.addValidator('secondsat', {
    validateString: function(value) {
      return isCorrectDate(new Date(value));
    },
    messages: {
      en: 'Not valid date.'
    }
  });
  $('.dateRange input').attr('data-parsley-secondsat','');

  });

 

0 0
replied on June 4

Hi Vanessa

Did you figure this out. I'm not great at some of the coding so I haven't been able to see how to modify your code to get the outcome you wanted.

What I was able to do in testing was in the form to create a hidden field where it subtracted 2 weeks from the selected Beginning date and then use that as the .periodbegin to get to similar results.

0 0
replied on June 4

I didn't get exactly what I wanted, but I got close using the code you provided in another answer stream.  Thank you for asking Steven.  I will try your suggestion and see if I can get it to work. smiley

0 0
replied on May 26

Thank you this is helpful smiley

0 0
replied on May 22 Show version history

This is some code I found from another post that I used to only allow the user to pick Sunday on the Date Picker. You would need to add the class myDate to your Date field

 

$(document).ready(function(){
 
  function isCorrectDate(n) {
    var t = n.getDay();
    var d = n.getDate();
    return (t==0);
  }
  function checkDate(n) {
    return[isCorrectDate(n),""];
  }
  function checkField(input, inst) {
    if ($(input).closest('li').hasClass('myDate')) {
      $(input).datepicker("option", {beforeShowDay: checkDate});
    }
  }
  $.datepicker.setDefaults( {beforeShow: checkField} );
  window.Parsley.addValidator('secondsat', {
    validateString: function(value) {
      return isCorrectDate(new Date(value));
    },
    messages: {
      en: 'Not valid date.'
    }
  });
  $('.myDate input').attr('data-parsley-secondsat','');

  });

1 0
replied on June 4 Show version history

Hey Steve! That code is super useful for a similar thing I'm trying to accomplish with one of my own forms. Which portion of that script restricts it to Sundays? I'm looking to restrict the date picker for one field to a Friday, and for another field to a Thursday. 

Edit: Nevermind, I answered my own question without breaking things! For anyone else that's curious, this is the line that needs modified to change the day of the week:

return (t==0);

Where:

  • 0 = Sunday
  • 1 = Monday
  • 2 = Tuesday
  • 3 = Wednesday
  • 4 = Thursday
  • 5 = Friday
  • 6 = Saturday

 

Thanks!

2 0
replied on June 4

Okay, so changing the day of the week was easy. Modifying this code to apply to two different fields with two different day restrictions is not as easy, at least for me. Could anyone provide any assistance?

0 0
replied on June 4

Hey Steve! That's not exactly what I'm looking to accomplish. The two date fields don't need to be based on one another at all, I just need to restrict one of them to only allow Fridays to be selected, while also restricting another date field to only allow Thursdays. Using the code you provided for Vanessa I was able to tweak it to get it to work on another day other than Sunday, but tweaking it to work for two fields with two different dates is proving to be challenging.

0 0
replied on June 4

I think we need a more experienced coder for this :) @JimBrannen

0 0
replied on June 4 Show version history

This isn't probably the most efficient way of doing this but put the code in the form twice then just use a different css class on each date field (i.e. Fri and Thurs).  Then change the return to the 4 for Thurs and 5 for Fri. Also be sure to change the class at the bottom to correspond with the correct return day (see highlight in pic below).

1 0
replied on June 4

Appreciate it Steve :)

Took a stab at it and everything seems to work on my end. This should work:

$(document).ready(function() {
  function checkField(input) {
    var day = parseInt($(input).attr("data-parsley-date-day-restriction"));
    console.log(input);
    switch(day) {
      case 0: $(input).datepicker("option", {beforeShowDay: function(d){return [d.getDay()==0,""];}});
        break;
      case 1: $(input).datepicker("option", {beforeShowDay: function(d){return [d.getDay()==1,""];}});
        break;
      case 2: $(input).datepicker("option", {beforeShowDay: function(d){return [d.getDay()==2,""];}});
        break;
      case 3: $(input).datepicker("option", {beforeShowDay: function(d){return [d.getDay()==3,""];}});
        break;
      case 4: $(input).datepicker("option", {beforeShowDay: function(d){return [d.getDay()==4,""];}});
        break;
      case 5: $(input).datepicker("option", {beforeShowDay: function(d){return [d.getDay()==5,""];}});
        break;
      case 6: $(input).datepicker("option", {beforeShowDay: function(d){return [d.getDay()==6,""];}});
        break;
      default:
    }
  };
  $.datepicker.setDefaults({beforeShow: checkField});
  window.Parsley.addValidator("dateDayRestriction", {
    validateString: function(val, day) {
      return ((new Date(val)).getDay()+1)%7 == parseInt(day);
    },
    messages: { en: "Invalid day of the week." }
  });
  $(".date-restriction-sun input").attr("data-parsley-date-day-restriction","0");
  $(".date-restriction-mon input").attr("data-parsley-date-day-restriction","1");
  $(".date-restriction-tue input").attr("data-parsley-date-day-restriction","2");
  $(".date-restriction-wed input").attr("data-parsley-date-day-restriction","3");
  $(".date-restriction-thu input").attr("data-parsley-date-day-restriction","4");
  $(".date-restriction-fri input").attr("data-parsley-date-day-restriction","5");
  $(".date-restriction-sat input").attr("data-parsley-date-day-restriction","6");
});

It includes classes for every day of the week. Simply apply the appropriate CSS class to the date field and it should filter the selection and display an error if it's an incorrect day of the week.

CSS classes:

  • date-restriction-sun
  • date-restriction-mon
  • date-restriction-tue
  • date-restriction-wed
  • date-restriction-thu
  • date-restriction-fri
  • date-restriction-sat
1 0
replied on June 4

Jim. You sir, are a saint. Thank you so much for this! It works perfectly and the way in which you laid that out, I can reuse it on any forms in the future. This is fantastic!!! Thanks once again, and have a great day!

And thank you Steve for summoning Jim :)

2 0
replied on June 4

Thank you Jim for stepping up on this. I am sure many will use this in the future

@SteveKnowlton

1 0
replied on June 4 Show version history

Okay, one small issue that I've since fixed: Despite the restrictions working properly, the error message is being generated still. When I restrict the dates to Fridays, only a manually entered date that falls on a Thursday will work. When I restrict the dates to Thursdays, only a manually entered date that falls on a Wednesday will work.

I did some testing, and it seems that removing the "+1" from line 26 of that code fixes the issue.

2 0
replied on June 5

Does your date picker week start on a Monday or on a Sunday? My code should work for starting on Sunday, and what you found is exactly what I would have suggested!

0 0
replied on June 5

It starts on a Sunday as well. Strange...

0 0
replied on June 5 Show version history

I think I found the solution. JavaScript dates are very finicky. When it was converting the field's value from "2020-06-05" to a Date, it was converting it to 00:00 of that date in GMT. For me, since I'm GMT-4, this was converting it to 2020-06-04 20:00 local time, hence the need for the +1.

Here's the updated code that converts it to local time and should work for everyone:

$(document).ready(function() {
  function checkField(input) {
    var day = parseInt($(input).attr("data-parsley-date-day-restriction"));
    console.log(input);
    switch(day) {
      case 0: $(input).datepicker("option", {beforeShowDay: function(d){return [d.getDay()==0,""];}});
        break;
      case 1: $(input).datepicker("option", {beforeShowDay: function(d){return [d.getDay()==1,""];}});
        break;
      case 2: $(input).datepicker("option", {beforeShowDay: function(d){return [d.getDay()==2,""];}});
        break;
      case 3: $(input).datepicker("option", {beforeShowDay: function(d){return [d.getDay()==3,""];}});
        break;
      case 4: $(input).datepicker("option", {beforeShowDay: function(d){return [d.getDay()==4,""];}});
        break;
      case 5: $(input).datepicker("option", {beforeShowDay: function(d){return [d.getDay()==5,""];}});
        break;
      case 6: $(input).datepicker("option", {beforeShowDay: function(d){return [d.getDay()==6,""];}});
        break;
      default:
    }
  };
  $.datepicker.setDefaults({beforeShow: checkField});
  window.Parsley.addValidator("dateDayRestriction", {
    validateString: function(val, day) {
      return (new Date(val+" 00:00")).getDay() == parseInt(day);
    },
    messages: { en: "Invalid day of the week." }
  });
  $(".date-restriction-sun input").attr("data-parsley-date-day-restriction","0");
  $(".date-restriction-mon input").attr("data-parsley-date-day-restriction","1");
  $(".date-restriction-tue input").attr("data-parsley-date-day-restriction","2");
  $(".date-restriction-wed input").attr("data-parsley-date-day-restriction","3");
  $(".date-restriction-thu input").attr("data-parsley-date-day-restriction","4");
  $(".date-restriction-fri input").attr("data-parsley-date-day-restriction","5");
  $(".date-restriction-sat input").attr("data-parsley-date-day-restriction","6");
});

 

0 0
replied on May 22 Show version history

Hi Vanessa,

Using JavaScript you should be able to set the "min" and "max" values of the date field to the datess that you want.

They should be in the yyyy-MM-dd format, like "2020-05-22".

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.