Limit date selection in date field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2017 06:28 AM
I would like to set up a date field (variable name: "access_expiration_date") so that if a user tries to select a date greater than six months from the current date, they will get an error message. I would imagine this is an easy script to write, but I'm a fairly new admin and I'm not quite sure how to do it.
Thanks!
Vince
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2017 07:00 AM
Hi VInce,
Following links should be helpful to you,
Find difference between two dates
Client script to validate start/end date fields
Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2017 10:43 AM
Create an onChange catalog client script on the expiration date variable and put this code in there
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var date=new Date(getDateFromFormat(g_form.getValue('opened_at'), g_user_date_time_format));
var future_date=new Date(new Date().getTime() + 60*24*180*60000);
if(date>future_date){
alert("selecet a different date");
g_form.setValue('access_expiration_date','');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2017 10:56 AM
Hi,
That didn't work. Below is what I had before. Am I on the right track?
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.hideFieldMsg('access_expiration_date', true);
var date=new Date(getDateFromFormat(g_form.getValue('opened_at'), g_user_date_time_format));
var future_date=new Date(new Date().getTime() + 6024180*60000);
website_url = newValue;
if(date>future_date){
g_form.showFieldMsg('access_expiration_date','Maximum allowed is six months.');
g_form.clearValue('access_expiration_date');
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2017 12:29 PM
use this code
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.hideFieldMsg('access_expiration_date', true);
var date=new Date(getDateFromFormat(g_form.getValue('access_expiration_date'), g_user_date_format));
var future_date=new Date(new Date().getTime() + 60*24*180*60000);
if(date>future_date){
g_form.showFieldMsg('access_expiration_date','Maximum allowed is six months.');
g_form.clearValue('access_expiration_date');
}
}