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

Question

Question

Calendar date restrictions in Forms with Javascript?

asked on July 11, 2014

HI,

 

I am still new to Javascript, and I'm attempting to put some restrictions on a Date field in Forms.

 

Is it possible to restrict the pop-up calendar to only show up to 90 days in the future?

 

If not, I would like to compare the current date vs. the user's selected date. If the difference between the two is more than 90 days, then disable the 'submit' button and display an error message. Is this possible?

 

1 0

Answer

SELECTED ANSWER
replied on August 29, 2014 Show version history

Okay, so if you take a date field and set it to default to the current date, and assign it a CSS Class of currentDate, then add the following to the CSS section:

.currentDate
{
  display:none;
}

Then, Take your date field you want to limit to 90 days and add the CSS Class of SelectDate to it. Then add in the Javascript section the following:

$(function() {
  $(document).ready(function () {
    var currentDate = $('.currentDate input').val();  // Gets Current Date Information from Hidden Field
    var dateSplit = [];
    dateSplit = currentDate.split("/");
    var newMonth = parseInt(dateSplit[0]) + 3; // Get Month + 3
    var AddYear = 0; // Year Offset
    if (newMonth > 12){  // Determine if not a valid month
      newMonth -= 12;    // Make newMonth valid
      AddYear = 1;       // Increment year offset
    }

    var newYear = parseInt(dateSplit[2]) + AddYear;  // Calculate the Year
    var maxDate = (newYear+"-"+newMonth+"-"+dateSplit[1]);
    $('.SelectDate input').attr('max',maxDate);
 
  });
});

 

This has been working for me in my tests. The only thing is this is an approximation. Instead of taking 90 days forward, it's just calculating 3 months into the future. That is not however to mean it is impossible to calculate to 90 days, but I felt that this would get you in the ballpark of what you wanted to accomplish.

 

You can make further modifications like making sure that you do not assign February an end date of 29 or 30 or 31 by adding some more stuff in at line 12

0 0

Replies

replied on July 14, 2014

If you want to compare the current vs selected dates, you can use JavaScript. My answer to this question should get you started. If you have more questions or are having trouble getting it to work, let me know.

1 0
replied on July 14, 2014

Hi Patrick,

 

I know as part of the standard build you can specify this in the edit window as shown here. (Year Range)

 

However it seems the minimum you can set this to is 1 year. Perhaps this can be referenced differently in a script. Certainly would be a valid feature request to have this display in days as well as years?

 

Perhaps LF can advise further on this? (I'm not a coder) wink

 

Hope this helps!

0 0
replied on August 29, 2014

Thank you for the help, everyone. I had originally settled for disabling the submit button if the selected date was > 90 days into the future.

I am going to test this now, Kenneth! Thank you.

0 0
replied on September 10, 2014

how did tests go?

0 0
replied on September 11, 2014

It worked well! That's exactly what I had in mind in my original question.

 

Thank you.

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

Sign in to reply to this post.