Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Restrict the Date field from allowing future dates

sarahkapasi
Giga Expert

How can I restrict a date field from allowing to enter future date?

Thank you,

Sarah 

1 ACCEPTED SOLUTION

Paul Curwen
Giga Sage

Try this On Change Client Script for your date field that you want to check:

 

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 Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul

View solution in original post

5 REPLIES 5

Paul Curwen
Giga Sage

Try this On Change Client Script for your date field that you want to check:

 

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 Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul

Thank you for your code 

i used it , 🙂 

sarahkapasi
Giga Expert

Thank you!  That worked!

Rosy
Kilo Contributor

I am getting  'getDateFromFormat' is undefined error in service portal. can you please help me with the code that works in service portal?