- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2018 10:02 AM
Hello all,
We have a couple of catalog items that use the date variable type and we want to restrict the user's from selecting a date in the past. Is this possible?
Also, we have Start Date and End Date on one our catalog items, does anyone know if its possible to restrict the end date to show dates past the selected start date?
Any suggestions are greatly appreciated!
Thanks,
Grace
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2018 11:12 AM
Hi,
Kindly use this code:
Suppose the variable/field name is 'current_date' on the catalog item/form
---'OnChange' Client Script
--field name : current_date
-Script :
function onChange(control, oldValue, newValue, isLoading)
{
if(isLoading)
{
return;
}
//start the validation
if(newValue != '')
{
var ga = new GlideAjax('ConfirmDate');
ga.addParam('sysparm_name', 'chkCurrDate');
ga.addParam('sysparm_date',g_form.getValue('current_date'));
ga.getXML(NewParse);
}
function NewParse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'false'){
alert("Date selected is a past entry.");
g_form.setValue('current_date', ''); // Setting the variable as empty
}}}
----Script include :-
---Name : ConfirmDate
--Client callable : true
-Script :
var ConfirmDate= Class.create();
ConfirmDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
chkCurrDate : function() {
var start = this.getParameter('sysparm_date');
var currDay = gs.now();
if(start < currDay){
return false;
}
else
{ return true; } } });
regards,
Munender
**Kindly hit correct/helpful if found useful

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2018 10:08 AM
For Portal only;
Add onChange client script on start date and add onChange client script on end date
Start
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '')
return;
// Date format
var dateFormat = g_user_date_format.toUpperCase();
// Make sure start date is valid
if (!(new moment(newValue, dateFormat).isValid())) {
alert('The date entered is not a valid fromat, please use the date selector or enter using the format: ' + dateFormat);
g_form.setValue('start_date', '');
}
else {
// Check that start is not in the past
if (moment().diff(newValue, 'days') > 0) {
alert('The start date must be on or after today.');
g_form.setValue('start_date', '');
}
}
}
End
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '')
return;
// Date format
var dateFormat = g_user_date_format.toUpperCase();
// Make sure end date is valid
if (!(new moment(newValue, dateFormat).isValid())) {
alert('The date entered is not a valid fromat, please use the date selector or enter using the format: ' + dateFormat);
g_form.setValue('start_date', '');
g_form.setValue('end_date', '');
}
else {
// Variables used
var startStr = g_form.getValue('start_date');
var startDate = new moment(startStr);
// Check that end is not in the past
if (startDate.diff(g_form.getValue('end_date'), 'days') >= 0) {
// The end date cannot be the same or less than the start date
alert('The end date must occur after the start date.');
g_form.setValue('end_date', '');
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2018 10:52 AM
Thanks Mike this is helpful, but when testing I was still able to select dates in the past without getting an error message.
The only difference I noticed was when submitting both fields were highlighted with a red border

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2018 11:09 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2018 11:12 AM
Hi,
Kindly use this code:
Suppose the variable/field name is 'current_date' on the catalog item/form
---'OnChange' Client Script
--field name : current_date
-Script :
function onChange(control, oldValue, newValue, isLoading)
{
if(isLoading)
{
return;
}
//start the validation
if(newValue != '')
{
var ga = new GlideAjax('ConfirmDate');
ga.addParam('sysparm_name', 'chkCurrDate');
ga.addParam('sysparm_date',g_form.getValue('current_date'));
ga.getXML(NewParse);
}
function NewParse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'false'){
alert("Date selected is a past entry.");
g_form.setValue('current_date', ''); // Setting the variable as empty
}}}
----Script include :-
---Name : ConfirmDate
--Client callable : true
-Script :
var ConfirmDate= Class.create();
ConfirmDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
chkCurrDate : function() {
var start = this.getParameter('sysparm_date');
var currDay = gs.now();
if(start < currDay){
return false;
}
else
{ return true; } } });
regards,
Munender
**Kindly hit correct/helpful if found useful