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

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Edited :- it will allow users to select only dates after 15 minutes from current date.

Logic for that :--

var dateTime1 = new GlideDateTime();

dateTime1.addSeconds(900);

if(newValue<dateTime1){

// Your error 

}

 

Assuming your date field is type of 'Date and Time' type.

 

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

Gunjan Kiratkar
Kilo Patron
Kilo Patron

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

//Type appropriate comment here, and begin script below

var date = new GlideDateTime();

date.addSeconds(900);
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');
}


}


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

I am getting an error below 

find_real_file.png

 

Script

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

//Type appropriate comment here, and begin script below

var date = new GlideDateTime();

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

if (sd < date) {

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


}

Oh my mistake

We can't use GlideDateTime in client side.

We need to call the script include and implement the same logic in that. If it satisfies the condition then return false otherwise return true from script include.

Then in If loop of client script we can check if we get false then show error.

 

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