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

This solution of 2 UI Policies worked perfectly for me. Thanks !.

Also, thank you for the script which you provided.