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

Question

Question

restrict date input to greater than 365 days

asked on October 28, 2015

Form field StatusCodeDate,

I have been reading other questions from users trying to restrict the date input. I tried one but it seemed to only be checking the year and not down to the day. I need users to enter a statuscodeDate and have a warning and submit button disabled when the date is within the last 365 days. So the date needs to be older than 365 days to the day. I am not a programmer...so help with the Java would be appreciated.

 

If date is less than 365 days old - warning message, disable submit button.

If date is greater than 365 days old allow date to be used and submitted.

1 0

Answer

SELECTED ANSWER
replied on October 28, 2015 Show version history

You can use the solution from this thread as an example and just slightly modify it so the maximum date value allowed is one year ago from the current date.

$(document).ready(function () {
    
  var todaysDate = new Date(); // Gets today's date
    
  // Max date attribute is in "YYYY-MM-DD".  Need to format today's date accordingly
    
  var year = todaysDate.getFullYear() - 1;  // YYYY - subtract 1 to enforce 365 day rule
  var month = ("0" + (todaysDate.getMonth() + 1)).slice(-2);  // MM
  var day = ("0" + todaysDate.getDate()).slice(-2);  // DD

  var maxDate = (year +"-"+ month +"-"+ day);  // Results in "YYYY-MM-DD" for today's date 
    
  // Now to set the max date value for the calendar to be today's date
  $('.statuscodedate input').attr('max',maxDate);
 
});

This assumes your date field uses the CSS class name, statuscodedate

1 0

Replies

replied on October 28, 2015

Hi there,

You can achieve this with custom javascript.

0 0
replied on November 2, 2015

Thank you, I was able to tweak it a bit. I really appreciate your help!!!

 

$(document).ready(function () {
    
  var todaysDate = new Date(); // Gets today's date
    
  
  
  // Max date attribute is in "YYYY-MM-DD".  Need to format today's date accordingly
    
  var year = todaysDate.getFullYear() -1;  // YYYY - subtract 1 to enforce 365 day rule
  var month = ("0" + (todaysDate.getMonth() + 1)).slice(-2);  // MM
  var day = ("0" + todaysDate.getDate()).slice(-2) -1;  // DD

  var maxDate = (year +"-"+ month +"-"+ day);  // Results in "YYYY-MM-DD" for today's date 
    
  // Now to set the max date value for the calendar to be today's date
  $('.statuscodedate input').attr('max',maxDate);
 
  
  
  
});

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

Sign in to reply to this post.