How can I restrict the user not to select future date on a date variable on a catalog form

Venkat141
Tera Contributor

How can i restrict to user not to select future dates on a date variable on Service Catalog form

 

 

@Ankur Bawiskar 

@Saurav 

@shloke04 

@Anil Lande 

@Maik Skoddow 

@Jaspal Singh 

@Sandeep Dutta 

@Chandra Sekhar Maganty 

@Musab Rasheed eed

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

no complex script required

Use Catalog UI policy

Start Time [After] Today

find_real_file.png

 

function onCondition() {

	// valid
	alert('Start date cannot be in future');
	g_form.showErrorBox('start_date_time','Invalid');
	g_form.setMandatory('start_date_time', true);

}

find_real_file.png

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Musab Rasheed
Tera Sage
Tera Sage

Hi @Pavan Ramireddy ,

Try below and add your field accordingly.

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;

            }

}

Please hit like and mark my response as correct if that helps
Regards,
Musab

Chandra Sekhar6
Tera Guru

Try this onChange Client Script on date field that you want to verify:

 

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

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

      return;

   }

            //current date

            var date= new Date();

            var date1= formatDate(date, g_user_date_format);

            var date2= getDateFromFormat(date1, g_user_date_format);

            var date3= getDateFromFormat(newValue, g_user_date_format);

 

            if (date3 > date2) {

                        alert(“Date should be in the past”);              

                        return false;

            }

}

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

no complex script required

Use Catalog UI policy

Start Time [After] Today

find_real_file.png

 

function onCondition() {

	// valid
	alert('Start date cannot be in future');
	g_form.showErrorBox('start_date_time','Invalid');
	g_form.setMandatory('start_date_time', true);

}

find_real_file.png

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur

 

That's a classic, reactive solution but I'm not sure if it really answers on the question.

What if the goal is to actually prevent the future dates from being selectable in the first place (as opposed to letting the user select them just to be alerted that this was not a valid selection) ? IMHO, if the invalid options are not selectable then we are giving the user better experience comparing to alerting them "Wrong date!".

How to achieve that (without fiddling around with the DOM - not a best practice).

 

//Rafal