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

Question

Question

How to disable weekends on date time field

asked on May 19, 2023

I have a form where a user can request a date to discontinue services. I would like to disable the ability to select weekends if possible since we are not open on weekends. Any information will be appreciated. Thank you for your time.

Screenshot 2023-05-19 122107.png
0 0

Answer

SELECTED ANSWER
replied on May 19, 2023 Show version history

This will do it.

Add the class "myDate" to your date field and copy in this JavaScript. 

I want to note that this is not my original code. A couple years ago, someone here helped me with code that would limit my datepicker to only Sunday, and all I've done here is tweak that a bit for you.

I hope this helps!

/*Limits date selection exclude weekends*/
$(document).ready(function(){
  function isCorrectDate(n) {
    var t = n.getDay();
    var d = n.getDate();
    return (t!=0 && t!=6);
  }
  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','');

 });

 

0 0
replied on May 19, 2023

Thank you. It works amazingly in Classic Designer, but can't seem to get it working in Layout Designer (the current form I have). 

1 0
replied on May 19, 2023

Given Matthew's response below, I think you'll have to stick with Classic Designer to make this work (unless more JS functionality is added to the new designer in the future).

0 0
replied on May 19, 2023

That's correct.  This is not going to be possible in the Layout Designer.

Javascript written for the Classic Designer cannot usually be copied directly to the Layout Designer as the structure of the form is completely differently.

Additionally, the Layout Designer runs the Javascript within a sandboxed iFrame, so it cannot directly modify components of the form.  It can only make changes that have been made available via the LFForm interface.

So this is not going to be possible on the Layout Designer unless a future release includes the ability to do this via the LFForm interface.

1 0

Replies

replied on May 19, 2023 Show version history

Which designer are you using?  The Forms Layout Designer (New Designer) or the Classic Forms Designer?

You're probably not going to be able to achieve this on the Forms Layout Designer with the current functionality available.  The Javascript runs within a sandboxed iFrame and cannot directly modify fields, and the default structure of the field doesn't have the ability to be configured in the way you would need.

But there are several examples here on LFAnswers with the Classic Forms Designer to achieve this kind of functionality using Javascript/JQuery.  Such as this one, this one, this one, this one, and this one.  

2 0
replied on May 19, 2023

I should note that my response includes code that was written in v10 and has been tested to work with the Classic Designer in v11.

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

Sign in to reply to this post.