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

Need to restrict Date field

Surya_Prakash
Tera Contributor

Hi All,

 

I have a requirement to restrict a date field to be not more than 2years from another date field. Example, If the field Date_A is '25-05-2023' then we should restrict to select only up to '24-05-2025' in Date_B field. Can you please help to achieve this.

 

TIA,

Surya. P

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @Surya_Prakash ,

 Please use Catalog UI Policy which needs no/Less coding:

https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-...

Just adjust the date to be "@ years" for your requirement.

 

View solution in original post

5 REPLIES 5

Community Alums
Not applicable

Hi @Surya_Prakash ,

 Please use Catalog UI Policy which needs no/Less coding:

https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-...

Just adjust the date to be "@ years" for your requirement.

 

Manmohan K
Tera Sage

Hi @Surya_Prakash ,

 

You can use below on change client script to do the vaildation

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	var today_dateStr = formatDate(new Date(),g_user_date_time_format);
	var days_from_today = new Date();

	//add 730 days to today's date.
	days_from_today.setDate(days_from_today.getDate()+730);
	var days_from_todayStr = formatDate(days_from_today,g_user_date_time_format);

	var todayNum = getDateFromFormat(today_dateStr,g_user_date_time_format);
	var daysNum = getDateFromFormat(days_from_todayStr,g_user_date_time_format);

	var selected_dateNum = getDateFromFormat(newValue,g_user_date_time_format);

	if(selected_dateNum < todayNum || selected_dateNum > daysNum) {
		g_form.addErrorMessage('Selected date should be between Today and 2 years from Today');
                g_form.clearValue('your field name');
	}
}

Eeshamayee kon1
Tera Expert

Hello @Surya_Prakash ,

Here in the below example, I am adding 2 years in start date field and aborting the action if end date selected is more than startdate+2 years in Business rule.

 

Eeshamayeekon1_0-1684934063341.png

You can verify this and use this in your scenario.

Please mark as Helpful if my solution is useful.

 

Thank you.

Surya_Prakash
Tera Contributor

Hi @Community Alums,

 

Thanks for that, it works fine now.

 

Thanks,

Surya. P