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

Sanjeev Kumar1
Kilo Sage

Hi,

It's good if user select date from calander only.

So that all user use same formate. To achive this make your date field read only. 

Maybe but choosing date format is for user convenient , so we are not going to make that as read only.

 

I have tried  getdatefromformat(), but it shows as browser console issue.

Hi,

See it 

@ https://docs.servicenow.com/bundle/london-application-development/page/script/useful-scripts/concept/c_ValidateDateAndTime.html

it's provided by ServiceNow.

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,

Can you show me Script include?

Thanks,
Ashutosh