Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to select only future date in date field using onsubmit client script

sriram7
Tera Contributor

Hi Team,

 

I have date field called return date. In that date field I need to select only present and future dates.

I can achieve this using ui policy, but I want to using client script.

11 REPLIES 11

Manmohan K
Tera Sage

Hi @sriram7 

 

You can use below on change client script

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var selectedDateNum = getDateFromFormat(newValue,g_user_date_time_format); 
   var today_date = new Date();
   var today_dateStr = formatDate(today_date, g_user_date_time_format); 
   var todayDateNum = getDateFromFormat(today_dateStr, g_user_date_time_format);
   if(selectedDateNum < todayDateNum) {
     g_form.addErrorMessage("Selected date cannot be less than today's date");
    } 
}

 

I want to populate error message in when i am submitting catalog form

@sriram7 ,

 

You can create a on submit client script in that case with below code

 

 

function onSubmit(){

  var dateVal=g_form.getValue('your date field');
   var selectedDateNum = getDateFromFormat(dateVal ,g_user_date_time_format); 
   var today_date = new Date();
   var today_dateStr = formatDate(today_date, g_user_date_time_format); 
   var todayDateNum = getDateFromFormat(today_dateStr, g_user_date_time_format);
   if(selectedDateNum < todayDateNum) {
g_form.addErrorMessage("Selected date cannot be less than today's date");
return false;
    } 
return true;
}

 

  

Hi @Manmohan K ,

 

If I am selecting tomorrows date it is throwing error

sriram7_0-1685098489200.png

 

sriram7_1-1685098556025.png