The CreatorCon Call for Content is officially open! Get started here.

Limit date selection in date field

vcaracci75
Tera Expert

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

15 REPLIES 15

Ankur Bawiskar
Tera Patron
Tera Patron

Hi VInce,



Following links should be helpful to you,


Find difference between two dates


Date Validation in ServiceNow


Client script date validation


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


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Abhinay Erra
Giga Sage

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','');


      }


}


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');


}


}


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');


             


      }


     


}