Validation of a date variable in a catalog client script

Mike Newlands
Tera Contributor

Hi! - I have a date field variable named change_date in a catalog item. I want to send the user an error message if the date they enter is less than the current date plus 3 days - I presume I need to do this in a catalog client script but unsure of the logic to use - Thanks!

1 ACCEPTED SOLUTION

@Mike Newlands 

In the Execute if true script do this

1) clear the variable and make it mandatory

2) This will ensure the user gives valid date before submission

g_form.clearValue('variableName'); // your variable name here

g_form.setMandatory('variableName', true); // your variable name here

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

8 REPLIES 8

Bob Fulkerson
ServiceNow Employee
ServiceNow Employee

Hi Mike,

You could certainly use a client script to achieve this.  You would need to leverage GlideAjax to pass your date to a script include where you can use the GlideDateTime API to do the validation.  See the following for details on those methods:

https://developer.servicenow.com/dev.do#!/reference/api/quebec/client/c_GlideAjaxAPI

https://developer.servicenow.com/dev.do#!/reference/api/paris/server/no-namespace/c_APIRef#r_ScopedGlideDateTimeGlideDateTime

For your case however, I think you can go with a simpler technique of using a Catalog UI policy and build your validation condition using the UI policy's condition builder.  If the condition evaluates to true, use the script field to throw a form error.  Here is a SS of a mock up of that:

 

find_real_file.png

 

 

 

Bob Fulkerson
ServiceNow Employee
ServiceNow Employee

Script to display the error:

Thanks Bob!

 

I added the ui policy and it works fine & sends an error message, but even though the date is 'invalid' (less than today + 3) it still lets me submit the request. How can the submission be 'stopped' if the value entered into the date field is invalid?

 

Thanks, Mike

@Mike Newlands 

In the Execute if true script do this

1) clear the variable and make it mandatory

2) This will ensure the user gives valid date before submission

g_form.clearValue('variableName'); // your variable name here

g_form.setMandatory('variableName', true); // your variable name here

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader