Restrict user to select a time in the future.

Ramya33
Tera Contributor

In my incident form, there is a field of 'date/time' format. Date validation(i.e restriction on selecting a date in the future) has been done. Kindly help on how to restrict a user to select a time in the future.

 

Thanks!

9 REPLIES 9

Ct111
Tera Sage

Hi Ramya,

 

You can do something like on an  onChange client script on the change of that date field.

 

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

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

      return;

   }

            //current date

            var currentDateObj = new Date();

            var currentDateStr = formatDate(currentDateObj, g_user_date_format);

            var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_format);

            var startDateNum = getDateFromFormat(newValue, g_user_date_format);

 

            if (startDateNum > currentDateNum) {

                        alert(“Date is in the future”);              

                        g_form.clearValue("your_date_field");

                        return false;

            }

}

 

If you want to see example you can refere below link where user has achieved the same.

https://community.servicenow.com/community?id=community_question&sys_id=1e3c0e52db7c5740b61ff3231f96...

 

Mark my ANSWER as CORRECT and also HELPFUL if it helped.

Ramya33
Tera Contributor

I want to restrict the user to select 'time' in the future. For example, if its 11:00AM when the user is raising a incident, he cannot select a time at 4:00PM on the same day. He should be able select a time before 11:00AM but not after 11:00AM.

Have you tried the code yet

Ramya33
Tera Contributor

yes, i tried..  but it is not working.