user shouldn't select date more than 6 months from today

vellanki divya
Kilo Guru

hello folks,

please do help me with the requirement

i have a calender field where the user shouldnot select date more than 6months.

i have tried using UI policy but didnt work perfectly.

the client need exactly 6 months.

1 ACCEPTED SOLUTION

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

}

var datesplit = (newValue+'').split('-');
var date= new Date(datesplit[2],datesplit[1]-1,datesplit[0]);
var sixmonthdate = new Date();
var day = sixmonthdate.getDate();
sixmonthdate.setMonth(6);
//sixmonthdate.setDate(day+1); for increasing days in the same month
sixmonthdate.setDate(day);
if(date>sixmonthdate)
{
g_form.showFieldMsg('FieldName','You can only request an extension of up to 6 months maximum and cannot be extended on top of the users current expiry date','error');
}
}

View solution in original post

3 REPLIES 3

rahulpandey
Kilo Sage
Hi Divya, This can be achieved by calculating days difference between current date and selected date. You need to write an on change client script on the date field, and call a script include to do difference check. Refer this https://community.servicenow.com/community?id=community_question&sys_id=38c40be9dbd8dbc01dcaf3231f9619e3

Sri Harsha3
Tera Expert

You need to use the Client SCript field onChange of the date field. Below is the validation script

 

var date_Value = newValue;

var gdt = new GlideDateTime(date_Value + " 00:00:00");
gdt.addWeeks(24);
var test = gdt.getDate();

if(dateValue > test) {
gs.print("Beyond");
}

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

}

var datesplit = (newValue+'').split('-');
var date= new Date(datesplit[2],datesplit[1]-1,datesplit[0]);
var sixmonthdate = new Date();
var day = sixmonthdate.getDate();
sixmonthdate.setMonth(6);
//sixmonthdate.setDate(day+1); for increasing days in the same month
sixmonthdate.setDate(day);
if(date>sixmonthdate)
{
g_form.showFieldMsg('FieldName','You can only request an extension of up to 6 months maximum and cannot be extended on top of the users current expiry date','error');
}
}