Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

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

Anurag Tripathi
Mega Patron
Mega Patron

Hi Niccole



You can use the following to check that your field does not contain a date before the current.



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.');


// at this point you can choose to clear the field or make it flash


}


}


-Anurag

Hi Anurag,



That worked !



You say in your script that we can make it flash and clear.   I added the following to the script (found on SNow Wiki) to yours, but it doesn't appear to be working.   Did I use the wrong code?



if (dt < rightNow) {
   
    alert('The date value cannot be before the current date and time. Please correct.');
    clearValue(effective_date);
    flash("effective_date", "#FFFF00", 5);
   
    // at this point you can choose to clear the field or make it flash
   

      }


I don't think "5" is a value you can use with flash...



Think you only can use these:



The arguments for the flash method are as follows: arg1: tablename.fieldname arg2: RGB color or acceptable CSS color like "blue" or "tomato" arg3: integer that determines how long the label will flash.  


  • Use 2 for a 1-second flash
  • Use 0 for a 2-second flash
  • Use -2 for a 3-second flash
  • Use -4 for a 4-second flash

Do not provide this argument if you want the field label simply colored the specified color



From: Make a Field Label Flash - ServiceNow Wiki


Thanks for letting me know.   I changed the 5 to a 2, but it still didn't work.