Date validation in catalog client script?

gokulraj
Giga Expert

Hello team,

I have a requirement in change request if user selects the past planned date it should have to popup an alert message to choose the current or future date.

It is working fine for almost all users with my old script,

it fails when user choose the date format like mm/dd/yy in their profile 

my new script

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var startDateStr = g_form.getValue('start_date');
if(moment) {
var dateFormat = g_user_date_time_format;
var currentDate = new moment(g_form.getValue('start_date'),dateFormat);
alert('currentDate**' + currentDate + 'startDateStr**' +startDateStr);
if(startDateStr <= currentDate) {
alert('"Planned Start Date" must be set to a current or future time');
g_form.setValue('start_date', '');

}
}

}

Old script

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var currentTime = '';
var time = new GlideAjax('DateTime');
time.addParam('sysparm_name', 'getNowDateTime');
time.getXML(
function () {
currentTime = time.getAnswer();
if (newValue <= currentTime){
alert('"Planned Start Date" must be set to a current or future time');
g_form.setValue('start_date','');
}
}

);

}

my issue is my new script not able to identify the current date , it shows like below,

find_real_file.png

How to achieve this?

1 ACCEPTED SOLUTION

Hi,


pass both newvalue and current value to script include and go comparison there and just return true or false to client script.

Convert both to GlideDateTime and then compare.


Thanks,
Ashutosh

View solution in original post

11 REPLIES 11

The below is my script include:

var DateTime = Class.create();
DateTime.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getNowDateTime: function () {
return gs.nowDateTime();
},

getNowRelativeDateTime: function () {
var relativeDateTime = GlideDateTime(gs.nowDateTime());
relativeDateTime.addSeconds(300);
return relativeDateTime;
},
getNowRelativeFutureDateTime: function () {
var relativeDateTime = GlideDateTime(gs.nowDateTime());
relativeDateTime.addSeconds(-300);
return relativeDateTime;
}
});

Hi,


Correct this line:

 

var relativeDateTime = GlideDateTime(gs.nowDateTime());

 

TO

 

var relativeDateTime = new GlideDateTime(gs.nowDateTime());

 

Thanks,
Ashutosh

Hi ,

I have updated as like you mentioned,

script include;

var DateTime = Class.create();
DateTime.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getNowDateTime: function () {
return gs.nowDateTime();
},

getNowRelativeDateTime: function () {
var relativeDateTime = new GlideDateTime(gs.nowDateTime());
relativeDateTime.addSeconds(300);
return relativeDateTime;
},
getNowRelativeFutureDateTime: function () {
var relativeDateTime = new GlideDateTime(gs.nowDateTime());
relativeDateTime.addSeconds(-300);
return relativeDateTime;
}
});

 

Client script;

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var currentTime = '';
var time = new GlideAjax('DateTime');
time.addParam('sysparm_name', 'getNowDateTime');
time.getXML(
function () {

currentTime = time.getAnswer();
alert('newValue**' + newValue + 'currentTime**'+currentTime );
if (newValue <= currentTime){
alert('"Planned Start Date" must be set to a current or future time');
g_form.setValue('start_date','');
}
}

);

}

 

Please find the below screen shot;

find_real_file.png

Clearly the new value is greater than the current time but still it satisfy the if condition and showing alert like below

find_real_file.png

Already I have choosed the future date, this is the issue I am exactly facing

Hi,


pass both newvalue and current value to script include and go comparison there and just return true or false to client script.

Convert both to GlideDateTime and then compare.


Thanks,
Ashutosh

How to do that, will you please share the script?