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

Question

Question

Date field - how to set minimum 3 business days in advance

asked on June 19, 2017 Show version history

Hi there,

 

I'm using Forms 10.1, working on a Record of Employment process. I want to set a date field for the last day the employee will work. The restriction on this field is that the date must be no sooner than 3 days in advance. So for example, if the form is submitted on Monday, June 19th, the soonest date a user can fill in would be Thursday, June 22nd. I also need it to not count weekends, as we're dealing with business days.

 

Could anyone provide me a script that could do something like this? Thanks for any response!

0 0

Answer

SELECTED ANSWER
replied on June 19, 2017

Here is a demo for Forms 10.1 (businessAdd function from https://github.com/leonardosantos/momentjs-business)

Add CSS class "myDate" on your date field.

//https://github.com/leonardosantos/momentjs-business/blob/master/momentjs-business.js
(function () {
  var moment;
  moment = (typeof require !== "undefined" && require !== null) &&
           !require.amd ? require("moment") : this.moment;

  moment.fn.businessAdd = function (days) {
    var signal = days<0?-1:1;
    days = Math.abs(days);
    var d = this.clone().add(Math.floor(days / 5) * 7 * signal, 'd');
    var remaining = days % 5;
    while(remaining){
      d.add(signal, 'd');
      if(d.day() !== 0 && d.day() !== 6)
        remaining--;
    }
    return d;
  };

}).call(this);

$(document).ready(function(){
  //Exclude weekend in date picker
  function isCorrectDate(n) {
    var t = new Date(n).getDay();
    return (t!=6 && 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} );
  
  //Minimum 3 business days in advance
  var date = moment().businessAdd(3);
  $('.myDate input').attr('min', date.format('YYYY-MM-DD'));
  
  //Validation for exclude weekend, for Forms 10.1
  $.webshims.ready('form-validators', function () {
    $.webshims.addCustomValidityRule('myDate', function (elem, value) {
      if (value && $(elem).closest('li').hasClass('myDate')) {
        return !isCorrectDate(value);
      }
      return false;
    }, 'Not valid date.');
  });
})

 

1 0
replied on June 20, 2017

Works perfectly, thank you!

0 0

Replies

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

Sign in to reply to this post.