- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2022 01:02 AM
Hello everyone,
The requirement is for the user to not be able to select a previous date, if they select a date not of the current date or onwards. It will show a red message saying "Please Select Current Date or On wards".
Is it possible to do using ajax or client scripts if so how should it be done?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2022 01:28 AM - edited 11-27-2022 01:29 AM
Hi @MelvinBulan ,
Please try adding the below script include and the client script, update the name for your "date_variable" in the client script, and see how you go.
Script Include:
var checkDateUtils = Class.create();
checkDateUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
dateDiff: function() {
var firstDate = this.getParameter('sysparm_first_date');
var secondDate = this.getParameter('sysparm_second_date') || gs.now();
return gs.dateDiff(secondDate, firstDate, true);
},
type: 'checkDateUtils'
});
Client Script - onChange date variable
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Start Date cannot be backdated
var dateAjax = new GlideAjax('checkDateUtils');
dateAjax.addParam('sysparm_name', 'dateDiff');
dateAjax.addParam('sysparm_first_date', newValue);
dateAjax.getXMLAnswer(processResponse);
}
function processResponse(answer) {
// If Start Date is in the past (Less than 0) show an error and clear the value
if(answer < 0) {
g_form.clearValue('date_variable');
g_form.showErrorBox('date_variable', 'Date cannot be in the past!');
} else {
g_form.hideErrorBox('date_variable');
}
}
Hope this helps!
If my answer helped you in any way or if this resolved your issue, please then mark it as helpful or correct accordingly to set this thread to solved.
BR.
BR.
Ishaan Shoor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2022 01:12 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2022 01:28 AM - edited 11-27-2022 01:29 AM
Hi @MelvinBulan ,
Please try adding the below script include and the client script, update the name for your "date_variable" in the client script, and see how you go.
Script Include:
var checkDateUtils = Class.create();
checkDateUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
dateDiff: function() {
var firstDate = this.getParameter('sysparm_first_date');
var secondDate = this.getParameter('sysparm_second_date') || gs.now();
return gs.dateDiff(secondDate, firstDate, true);
},
type: 'checkDateUtils'
});
Client Script - onChange date variable
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Start Date cannot be backdated
var dateAjax = new GlideAjax('checkDateUtils');
dateAjax.addParam('sysparm_name', 'dateDiff');
dateAjax.addParam('sysparm_first_date', newValue);
dateAjax.getXMLAnswer(processResponse);
}
function processResponse(answer) {
// If Start Date is in the past (Less than 0) show an error and clear the value
if(answer < 0) {
g_form.clearValue('date_variable');
g_form.showErrorBox('date_variable', 'Date cannot be in the past!');
} else {
g_form.hideErrorBox('date_variable');
}
}
Hope this helps!
If my answer helped you in any way or if this resolved your issue, please then mark it as helpful or correct accordingly to set this thread to solved.
BR.
BR.
Ishaan Shoor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2022 01:30 AM
Hello,
You do not require to write any script. You can achieve this using UI policy
Just write the condition as below:-
Please check the below article for the same for more detail:-
Please mark my answer as correct based on Impact.