how to restrict a date field to be selected as future date

VIKAS MISHRA
Tera Contributor

I want to restrict the selection of future date on a date field in service portal , as it should give a field message "Date cannot be future date" and then clear the field value.

I have tried the older code found on Servicenow community as mentioned below but not working.

 

Not Working this code.

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

//Type appropriate comment here, and begin script below


//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 cannot be future date");

g_form.clearValue("u_date_time_occurrence");

return false;

}

}

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Have you considered using a (Catalog) UI Policy to achieve date validations? There's almost no-code needed to achieve date validations this way. Have a look at an article I wrote on this:
No Code date validations thru (Catalog) UI Policies

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020-2021 ServiceNow Community MVP
2020-2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

2 REPLIES 2

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Have you considered using a (Catalog) UI Policy to achieve date validations? There's almost no-code needed to achieve date validations this way. Have a look at an article I wrote on this:
No Code date validations thru (Catalog) UI Policies

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020-2021 ServiceNow Community MVP
2020-2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Arkesh Kumar
Giga Guru

Hi

 

Create a On change client script as below

 

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

var ga = new GlideAjax('date_validation');
ga.addParam('sysparm_name','Currentdate');
ga.addParam('sysparm_date',newValue);
ga.getXMLWait();
var answer = ga.getAnswer();
//g_form.addInfoMessage("answer "+answer);

if(answer == "false"){
g_form.addErrorMessage("Required by date cannot be a past date");
g_form.clearValue("date_field_name");

}

}

 

Script include: make it client callable

 

var date_validation = Class.create();
date_validation.prototype = Object.extendsObject(AbstractAjaxProcessor, {

date:function(){
var today = gs.nowDateTime();
var termination = this.getParameter('sysparm_user_name');
var diff = gs.dateDiff(termination,today,true);
gs.addInfoMessage("diff "+diff);
return diff;
},

Currentdate:function(){
var date = false;
var today = gs.now();
var ReqDate = this.getParameter('sysparm_date');
//gs.addInfoMessage("Selected Date -"+ ReqDate);
if(today <= ReqDate)
{
date = true;
}

return date;


},
type: 'date_validation'
});

 

Thank You,

Arkesh