top of page

Experiencing challenges with ServiceNow support?

IKC provides professional, reliable assistance that covers gaps not supported by ServiceNow

and without the high costs of traditional services.

 

Starting from just $1,000

Ensuring Dates are in the Future: Validating Date Fields in ServiceNow Client Scripts

Updated: Mar 30


In ServiceNow, accurately capturing and validating data is paramount for maintaining data integrity and ensuring smooth process execution. A common requirement is to ensure that a date field on a form contains a date in the future. While server-side validation through Business Rules is an option, implementing this logic using Client Scripts provides immediate feedback to the user, enhancing their experience and reducing potential errors. This article will guide you through how to effectively validate date fields in ServiceNow Client Scripts to ensure the selected date lies in the future.


The Importance of Future Date Validation in ServiceNow


Consider various scenarios within ServiceNow where ensuring a future date is crucial. For instance, setting an expiration date for a temporary access request, scheduling a future maintenance window, or planning a project start date all necessitate that the selected date falls in the future. Implementing client-side validation for such fields provides real-time feedback to the user as they interact with the form. This prevents them from submitting records with invalid dates, saving time and effort in correcting data later.


Implementing Future Date Validation with a Client Script


A highly efficient way to validate if a date field in ServiceNow contains a future date is by using an onChange Client Script. This type of script executes whenever the value of a specified field changes, allowing for immediate validation.


Step-by-Step Implementation:


  1. Create a New onChange Client Script:

    Navigate to System Client Scripts and click New. Configure the client script as follows:

    • Name: (e.g., Validate Future Date)

    • Table: Select the table that contains the date field you want to validate.

    • UI Type: All

    • Type: onChange

    • Field name: Select the specific date field you want to validate (e.g., 'ends').

    • Active: True

    • Global: True (unless the script is specific to a particular view)

  2. Write the Client Script: In the Script field, paste the following code:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') { 
		return;
	}
 
	// Get the current date
	var currentDateObj = new Date();
	var currentDateStr = formatDate(currentDateObj, g_user_date_format);
	var currentDateNum = getDateFromFormat(currentDateStr,
	g_user_date_format); 

	// Get the selected date
	var selectedDateNum = getDateFromFormat(newValue,g_user_date_format); 

	// Compare the dates
	if (selectedDateNum <= currentDateNum) {
		g_form.showFieldMsg('YOUR_DATE_FIELD_NAME', 'Please ensure that
		the selected date is in the future!', 'error');
		g_form.setValue('YOUR_DATE_FIELD_NAME', ''); // Optionally clear the field 
		return false; // Prevent further processing or saving
	} else {
		g_form.hideFieldMsg('YOUR_DATE_FIELD_NAME', true); 
		// Clear any previous error message
	}
}
  1. Replace Placeholder: In the script, replace 'YOUR_DATE_FIELD_NAME' with the actual name (the backend name) of the date field you are validating. For example, if your date field is named "Expiry Date," its backend name might be 'u_expiry_date'.

  2. Save the Client Script: Click Save.


Explanation of the Script:


  • The onChange function is triggered whenever the value of the specified date field changes.

  • The script first checks if the form is loading or if the new value is empty. If so, it exits to avoid unnecessary processing.

  • It then gets the current date using new Date() and formats it using formatDate according to the user's date format (g_user_date_format). getDateFromFormat then converts this formatted string back into a numerical representation for easy comparison.

  • Similarly, it gets the selected date (newValue) and converts it to a numerical representation.

  • The core logic lies in the comparison: if (selectedDateNum <= currentDateNum). If the selected date is the same as or earlier than the current date, an error message is displayed to the user using g_form.showFieldMsg(). The date field is optionally cleared using g_form.setValue(), and return false; prevents any further processing or saving of the form until a valid future date is selected.

  • If the selected date is in the future, any previous error message for that field is cleared using g_form.hideFieldMsg().


Practical Examples and Use Cases


This client script can be adapted for various scenarios:


  • Request Management: Ensuring the "Requested Delivery Date" is not in the past.

  • Change Management: Validating that the "Planned Start Date" of a change is a future date.

  • Task Management: Making sure the "Due Date" of a task is set for a future time.

  • Catalog Items: When users are selecting dates for various requests, such as equipment return dates or service start dates.


Alternative Solutions


While the onChange client script provides immediate validation, here are a couple of alternative approaches:

  • UI Policies: You can achieve similar date validation using UI Policies. You would set a condition that checks if the selected date is not in the future and then use a UI Policy action to display an error message and potentially make the field mandatory until a valid date is chosen. This approach might be preferred by administrators who are less comfortable writing JavaScript code.

  • onSubmit Client Script: For a more robust solution, you could also implement a similar validation in an onSubmit client script. This script will execute when the user attempts to submit the form. While it doesn't provide immediate feedback like an onChange script, it acts as a final check before the record is saved.


Conclusion: Empowering Users with Real-Time Date Validation


Validating date fields to ensure they are in the future is a fundamental aspect of building user-friendly and error-free ServiceNow applications. By implementing an onChange client script as described, you can provide immediate feedback to users, guiding them to select valid future dates and improving the overall data quality within your ServiceNow instance. Remember to adapt the script to your specific date field name and consider the alternative solutions based on your specific requirements and preferences.

Experiencing challenges with ServiceNow support?

IKC provides professional, reliable assistance that covers gaps not supported by ServiceNow

and without the high costs of traditional services.

 

Starting from just $1,000

CONTACT

New Zealand HQ

Integrated Knowledge Consulting Office

Level 3, 93 Grafton Road

Auckland

South Korea

Integrated Knowledge Consulting Office

BMY역삼타워 6층

서울특별시 강남구 역삼동 678-10번지

 

info@ikconsulting.com

Thanks for submitting!

  • LinkedIn Social Icon

© Copyright 2025 Integrated Knowledge Consulting. All rights reserved.

bottom of page