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

Question

Question

restrict date field to a future date in Forms

asked on July 23, 2018

I want to be able to restrict the date field to only allow someone to select a date 7 days in the future or beyond? Any thoughts on if this is possible?

 

Thanks,

Lisa

2 0

Replies

replied on July 23, 2018 Show version history

Add a custom class to your date field, something like "futureDateOnly" then the following code will adjust the minimum date attribute that is used by the date picker.

$(document).ready(function(){
  // Get date
  var d = new Date();
  
  // Add 7 days (+ 1 to month is necessary because it uses a 0 index)
  var dmy = [d.getDate() + 7, d.getMonth() + 1, d.getFullYear()];

  // Format date
  for (var n = 0; n < 2; n++){
    if (dmy[n].toString().length < 2){
      dmy[n] = "0" + dmy[n];
    }
  }

  // Set the min date value for the date calendar
  $('.futureDateOnly input').attr('min',(dmy[2] + "-" + dmy[1] + "-" + dmy[0]));
});

 

1 0
replied on July 23, 2018

That was perfect. Thanks so much!

1 0
replied on July 30, 2018

This code was working when I tried it last week but now I'm getting the following error message. I believe it is because its July 30th and this is missing something to recognize the new month instead of just adding 7 days to today's date? It also won't allow the calendar to even pop up? Anybody have any idea on how to fix this?

Thanks, 

Lisa

1 0
replied on July 30, 2018

Try this instead. Turns out you need to add 7 days a different way to make sure it automatically moves over to the next month.

$(document).ready(function(){
  // Get date
  var d = new Date();

  // Add 7 days to date
  d.setDate(d.getDate() + 7);  

  // + 1 to month is necessary because it uses a 0 index
  var dmy = [d.getDate(), d.getMonth() + 1, d.getFullYear()];

  // Format date
  for (var n = 0; n < 2; n++){
    if (dmy[n].toString().length < 2){
      dmy[n] = "0" + dmy[n];
    }
  }

  // Set the min date value for the date calendar
  $('.futureDateOnly input').attr('min',(dmy[2] + "-" + dmy[1] + "-" + dmy[0]));
});

 

1 0
replied on July 30, 2018

Thank you, that seems to have corrected the issue!

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

Sign in to reply to this post.