Allow user to select date if is more than 15minutes

Zuri
Tera Expert

I want to allow user to select date and time if is more than 15 minutes from current date and time. Below onChange client script I am not able to select previous date but I am able to select anytime after current time. I want to allow only to select when time is 15 minutes or more from current time.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

//Type appropriate comment here, and begin script below

var date = new Date();
var sd = new Date(getDateFromFormat(g_form.getValue('date'), g_user_date_time_format));

if (sd < date) {

g_form.clearValue('date);
g_form.showFieldMsg('date', "Start date must be in the future.", 'error');
jslog('txt');
}


}

1 ACCEPTED SOLUTION

Is working but after I submit a form with correct time and go back and change time to less than 15 minutes is updating the form without any error

View solution in original post

28 REPLIES 28

Could you please help me with script include

Hi @Zuri ,  

 

Client Script : On change of start date  

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

//Type appropriate comment here, and begin script below
var ga = new GlideAjax('dateValidation');
ga.addParam('sysparm_name', 'getDate');
ga.addParam('sysparm_date', newValue);
ga.getXML(getData);

function getData(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'false') {
g_form.clearValue('start_date');
g_form.showFieldMsg('start_date', "Start date must be in the future.", 'error');
jslog('txt');
}
}

}  

 

find_real_file.png

 

Change the Field name to start date from follow up date

Script Include

 

:-  

 

 

var dateValidation = Class.create();
dateValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getDate: function() {
var date = this.getParameter('sysparm_date');

var currentDate = new GlideDateTime();
var currDate=currentDate.getDisplayValue();
currDate.addSeconds(900);
gs.info('currentDate' + currDate);
gs.info('date' + date);
if ( date< currentDate) {
return false;

} else return true;

},

type: 'dateValidation'
});  

 

 

find_real_file.png

 

The script is tested and working,

 

Please mark my answer as helpful/correct if it resolves your query.

 

Regards,

Gunjan Kiratkar

Consultant - ServiceNow, Cloudaction

Rising Star 2022

 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

Hi Zuri,

Did you get chance to implement my solution?

If it is working then please mark my answer as correct/helpful

 

Regards,

Gunjan


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

Is not picking up the time (currDate.addSeconds(900) 

Can you please send me your code 

 

Thanks,

Gunjan


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy