How can I limit a DATE field from being populated with invalid dates

Hola Ola
Giga Guru

Happy Friday All, 

 

How can I prevent users from populating a DATE field with a date No Less That 5 Business Days (7 Total days from today) and No More Than 10 Business days (14 Total days from today).

 

HolaOla_0-1726249266144.png

Thank you for your anticipated help. 

1 ACCEPTED SOLUTION

It Is working fine in my pdi @Hola Ola 

Refer below link

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

 

You can also write onChange Client script 

https://www.servicenow.com/community/itsm-forum/if-end-date-is-more-than-10-business-days-alert-shou...

 

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

View solution in original post

11 REPLIES 11

Ravi Peddineni
Kilo Sage

@Hola Ola 

 

Preventing date selection involves DOM manipulation which is not so good practice. Instead, create a UI policy with conditions as below: 

  1. Date<Date variable name> is before 7 days OR Date<Date variable name> is after 10 days

Create a Ui Policy action to clear the field value and display a field message if the above condition is met.

 

This will simply the logic and keep the solution clean.

 

Hit the Accept Solution button if the above solution helped.

Hello @Ravi Peddineni 

Thank you for your response. However, it is still not working as desired. Perhaps something that I'm doing wrong. 

 

I have the conditions this way:

Date field<variable_name>Relative>After>7>Days>From Now 

 

HolaOla_1-1726258066966.png

 

Also, I should mention that I want the days that can be chosen to ONLY be between 7days and 14days (just a 2-week period) - Nothing Less and Nothing More

 

Script:

function onCondition() {
g_form.clearValue(period_duration_required_for_the_requested_access);
g_form.showErrorBox('period_duration_required_for_the_requested_access', getMessage('Date should be 7 - 14 days in the future'));
}

 

Again, thank you for your assitance.

@Hola Ola 

 

There's a syntax error in your code. Below is the corrected code:

function onCondition() {
    g_form.clearValue('period_duration_required_for_the_requested_access'); //Did not use the single quotes in your code
    g_form.showErrorBox('period_duration_required_for_the_requested_access', getMessage('Date should be 7 - 14 days in 
    the future'));
}

 

Also make sure to use below conditions on UI Policy:

Ravindranath_0-1726259365168.png

 

Suggestion: To avoid scripting, Create a UI Policy Action and configure as shown below:

Ravindranath_1-1726259531761.png

 

Hit the Accept Solution button if the above solution helped you.

 

Hello @Ravi Peddineni 
Apologies for the bother, something is still not right here.

 

My UI Policy Action only has these fields, which limits my configuration option (unless I don't know how to get the extra fields as shown in your example)

HolaOla_0-1726266110346.png

 

Also, here are my Conditions, as shown by you: 

HolaOla_1-1726266176211.png

 

And my Script:

function onCondition() {
g_form.clearValue(period_duration_required_for_the_requested_access);
g_form.showErrorBox('period_duration_required_for_the_requested_access', getMessage('Date should be 7 - 14 days in the future'));
}

 

None seem to be working.