Validation on Date field to allow users to only select upto 7 days in future.

Sai Sachi
Kilo Contributor

Need to create a client script to validate that date I enter there should not be more than 30 days from today, There is another field called Impact. Is user selects any impact than date should not be more than 7 days from today.

 

1 ACCEPTED SOLUTION

Shubham Tipnis
Kilo Sage
Kilo Sage

Hi Sai,

 

You can do this using 2 UI policies as well. No need of using client script.

 

UI Policy for Impact is empty

Condition 1: Impact is empty AND //you can remove this condition if not needed.

Condition 2: Date before Today OR // My guess is you dont need dates from the past

Condition 3: Date relative after 30 days from now

 

UI Policy for Impact is NOT empty

Condition 1: Impact is empty AND //you can remove this condition if not needed.

Condition 2: Date before Today OR

Condition 3: Date relative after 7 days from now

 

 

Add below script(In Script section select Run Script is True and add code in Execute if True) in both the policies to clear the date field value and to show error message.(if needed)

 

 

function onCondition() {

g_form.clearValue('date_field_name');
g_form.showErrorBox('date_field_name', 'Select a date from today upto the next 30 days', 'error');

return false;
}

Sample snapshot:

find_real_file.png

 

 

 

Regards,

Shubham

Regards,
Shubham Tipnis
 ServiceNow Enthusiast
️ 3x Rising Star (2022–2024) – ServiceNow Community
 Sharing insights, use cases & real-world learnings from the Now Platform
 Always learning. Always building.

View solution in original post

5 REPLIES 5

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Have you considered using a (Catalog) UI Policy to achieve date validations? There's almost no-code needed to achieve date validations this way. Have a look at an article I wrote on this:
No Code date validations thru (Catalog) UI Policies

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Ha you beat me to this one. I actually saw this on your blog!!

-Anurag

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Here is an example of a no code solution.

You can use a ui policy with condition like below and in the script throw the error.

find_real_file.png

 

-Anurag

-Anurag

Shubham Tipnis
Kilo Sage
Kilo Sage

Hi Sai,

 

You can do this using 2 UI policies as well. No need of using client script.

 

UI Policy for Impact is empty

Condition 1: Impact is empty AND //you can remove this condition if not needed.

Condition 2: Date before Today OR // My guess is you dont need dates from the past

Condition 3: Date relative after 30 days from now

 

UI Policy for Impact is NOT empty

Condition 1: Impact is empty AND //you can remove this condition if not needed.

Condition 2: Date before Today OR

Condition 3: Date relative after 7 days from now

 

 

Add below script(In Script section select Run Script is True and add code in Execute if True) in both the policies to clear the date field value and to show error message.(if needed)

 

 

function onCondition() {

g_form.clearValue('date_field_name');
g_form.showErrorBox('date_field_name', 'Select a date from today upto the next 30 days', 'error');

return false;
}

Sample snapshot:

find_real_file.png

 

 

 

Regards,

Shubham

Regards,
Shubham Tipnis
 ServiceNow Enthusiast
️ 3x Rising Star (2022–2024) – ServiceNow Community
 Sharing insights, use cases & real-world learnings from the Now Platform
 Always learning. Always building.