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.

date field show error message

Community Alums
Not applicable

There is a "Date" field that is visible based on another field called "Request," which has three choices.

I need to implement validation on the Date field:

if a past date (earlier than today) is selected, an error message should appear, and the date field should be cleared. Additionally,

if the Request choice is changed, I do not want the previous error message to be displayed. However, after selecting a new date, if it is a past date, the error message should reappear.

 

 

 

3 REPLIES 3

OlaN
Tera Sage
Tera Sage

Hi,

A simple UI policy with UI policy actions can resolve this requirement.

Mark Roethof has written a good article about it here.

HIROSHI SATOH
Mega Sage

If you implement it in a client script:
※Replace "u_request_date" and "u_request_string" with the actual field names.

 

The onChang script of "request"

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

   //Type appropriate comment here, and begin script below
    var dateField = g_form.getValue('u_request_date');  // Replace 'your_date_field' with your actual Date field name

	var today_date = new Date();
	var today_date_str = formatDate(today_date, g_user_date_format);    

    // Check if the date is a past date
    if (dateField && new Date(dateField) < new Date(today_date_str)) {
        g_form.clearValue('u_request_date');
        g_form.showErrorBox('u_request_date', 'Please select a future date.');
    } else {
        g_form.hideErrorBox('u_request_date');
    }   
   
}

The onChang script of "date"

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

   //Type appropriate comment here, and begin script below
    g_form.hideErrorBox('u_request_date');
   
}

 

Harish Bainsla
Kilo Patron
Kilo Patron

Hi @Community Alums  you can achieve through Ui policy for reference check below link

https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-policies/ta-p/2297600