How do you prevent users from selecting a date in the past?

ngriffiths
Mega Expert

We have a requirement to capture an effective date on a record producer and I have currently built a Date/Time variable that will allow us to do so.   Now with that being said, the rest of the requirement is to prevent users from selecting a date/time in the past.

For example, today is 1/14/2016, therefore the user should NOT be able to select 1/10/2016 as a possible date.  

Essentially, we want any date leading up to the current date/time to be read only.   Is this a possibility and if so how do we go about it?   I am not a programmer, so if there is a script involved, which I am positive there is, it would be much appreciated if you can help with that as well.

Thanks!

Niccole

1 ACCEPTED SOLUTION

According to the SNow wiki article, the flash method is not supported by the Service Catalog.   Thus that is prob why no matter what I am changing it to, it still is failing.   Not a huge deal, it would have been nice to have the field flash a color, but the field is mandatory and clearing the value will force them to select a date before they can submit the request.



I very much appreciate all your help!



Here is the final script for anyone who is curious:



Catalog Client Script


Applies to: A Catalog Item


Type: onChange



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


     


      if (isLoading || newValue == '') {


             


              return;


             


      }


     


     


      var dt = getDateFromFormat(newValue, g_user_date_time_format);


     


      var rightNow = new Date();


     


      if (dt < rightNow) {


             


              alert('The date value cannot be before the current date and time. Please correct.');


              g_form.clearValue("effective_date");


     


      }


     


}


View solution in original post

14 REPLIES 14

g_form.flash("effective_date", "#FFFF00", 5);


-Anurag

Nope that didn't appear to work.   I also took Goran's suggestion above and changed 5 to a 2 and still nothing.


Hi,


I used the provided script which is very useful. This script is for Date/Time field. How can I modify this cript for [Date] field allowing user to select from the current date. Thanks! ï‚§ function onChange(control, oldValue, newValue, isLoading, isTemplate) {                 if (isLoading || newValue == '') {                                 return;                         }                         var dt = getDateFromFormat(newValue, g_user_date_time_format);                 var rightNow = new Date();                 if (dt < rightNow) {                                 alert('The date value cannot be before the current date and time. Please correct.');                 g_form.clearValue("effective_date");                 }         }


Hi Imran,



I'm not sure I am fully understanding your question.   Are you asking how to make it pull in the current date?   The script is currently doing that.   What it is preventing the user to do is back date.   So if the current date and time is 9/19/16 9:45AM it wont allow the user to select 9/18/16 as a date or 9/19/16 9:30AM.   Also, note that the script is pulling in the current time on your computer, but if you aren't submitting it right away, then a minute might have passed and as a result, when you try and submit, you are doing so with a time stamp that is in the past.   In other words you select the current date and time, but by the time you click submit a few minutes have gone by, the script thinks you are back dating.   We suggested to our users to always select a time that is an additional 5 minutes when submitting the ticket.   Hopefully that helps, but if you can further explain your question above I may be able to assist further.



Thanks!


Niccole


Hi Niccole,


Thanks for the clarification, highly appreciated.


Regards


Imran