The CreatorCon Call for Content is officially open! Get started here.

The date should be within the last 29 days!! If customer provides invalid date, present an ERROR message "Please choose a date within the 30 days. I need to use the onChange client script.

sakshi22
Tera Contributor

i have a date type variable and  date should be within the last 29 days!! If customer provides invalid date, present an ERROR message "Please choose a date within the 30 days. I need to use the onChange client script. Can someone please help me with it?

1 ACCEPTED SOLUTION

you cannot restrict dates in date picker.

since yours is date variable; update as this

onChange is running on which variable? it should be sap_restore_point right since user is selecting that

you want to compare restore point date with (today date-29)

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	var todayDate = new Date();
	todayDate.setDate(todayDate.getDate() - 29);
	var selected_dateNum = new Date(newValue);

	if(selected_dateNum.getTime() < todayDate.getTime() ) {
		g_form.addErrorMessage('Date should be 29 days from Expected date');
	}
}

Regards
Ankur

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

View solution in original post

7 REPLIES 7

Ashish21
Giga Expert

Hi Sakshi,

 

Please refer to this article.

 

Thanks,
Ashish Bhargava

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can easily handle this using UI policy

No Code date validations through (Catalog) UI Policies

Regards
Ankur

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

Hi ankur, i tried with ui policy but it its selecting the date so if for eg i am selecting todays date then 17th jan should not get selected and 16th should but it is currently not happening.

i tried an on change client script : can you please suggest where is it wrong?

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var restore_point = new Date(getDateFromFormat(g_form.getValue("sap_restore_point"), g_user_date_time_format));
restore_point.setDate(restore_point.getDate() - 29);
var restore_pointStr = formatDate(restore_point,g_user_date_time_format);
var restore_pointNum = getDateFromFormat(restore_pointStr,g_user_date_time_format);
var selected_dateNum = getDateFromFormat(newValue,g_user_date_time_format);

if(selected_dateNum < restore_pointNum ) {
g_form.addErrorMessage('Date should be 29 days from Expected date');
}
}

Hi,

you cannot remove those dates from date picker.

but can only validate those

What came in alert for those dates?

regards
Ankur

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