- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-05-2017 12:07 AM
Hi All,
We need to get the date value from the catalog variable and validate it in such a way that it should not allow user to submit the request for past dates. I have written the code, it is working fine only for system format. (YYYY-MM-DD). Below are the formats available in user profile. Based on user selected format it will be available the same in catalog date fields also.
Irrespective of the date format, we should not allow the user to submit the request for past dates.
Script Include:
validateDate: function()
{
var gdt = new GlideDateTime(gs.now());
var due_date = this.getParameter('sysparm_user_name');
gs.log("now date:"+gdt+" due date:"+due_date);
if(due_date> gdt)
{
gs.log("inside loop");
return "true";
}
Client Script On Change
var ga = new GlideAjax('validateDate');
ga.addParam('sysparm_name','validateDate');
ga.addParam('sysparm_user_name', newValue);
ga.getXML(DateParse);
function DateParse(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer != 'true')
{
alert('Past dates are not allowed to submit the request');
g_form.setValue('Requested_Date_Due','');
}
}
Any suggestions highly appreciable
Solved! Go to Solution.
- Labels:
-
Best Practices
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-05-2017 12:45 AM
Hey Mallikarjuna,
Use this instead of script include -
function onChange(control, oldValue, newValue, isLoading) {
if (!isLoading) {
if(newValue != '') {
//current date
var currentDateObj = new Date();
var currentDateStr = formatDate(currentDateObj, g_user_date_format);
var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_format);
//get start date
var startDateStr = g_form.getValue('sDate');
var startDateNum = getDateFromFormat(startDateStr, g_user_date_format);
if (startDateNum < currentDateNum) {
alert('You cannot select a date in the past.');
g_form.setValue('sDate', '');
}
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-05-2017 12:45 AM
Hey Mallikarjuna,
Use this instead of script include -
function onChange(control, oldValue, newValue, isLoading) {
if (!isLoading) {
if(newValue != '') {
//current date
var currentDateObj = new Date();
var currentDateStr = formatDate(currentDateObj, g_user_date_format);
var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_format);
//get start date
var startDateStr = g_form.getValue('sDate');
var startDateNum = getDateFromFormat(startDateStr, g_user_date_format);
if (startDateNum < currentDateNum) {
alert('You cannot select a date in the past.');
g_form.setValue('sDate', '');
}
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-05-2017 10:24 PM
Hi Nayan,
Problem is resolved now. Earlier I wasn't aware of this g_user_date_format property. I googled about this and got more info. Thanks for your response. Issues is fixed now.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-24-2020 04:12 AM
How to use the above functionality for date/time field?
Thanks in Advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-05-2017 12:45 AM
Hi,
Convert the date string into milliseconds and then validate it.
use this code to change the date in milli seconds.
var field1 = g_form.getValue('start_date');
var field1_ms = getDateFromFormat(field1, g_user_date_time_format);//in milli seconds