How to write the client script for datetime validation

PRAGHATIESH S
Tera Expert

Hi All,

I created two fields on the catalog item like start date and end date. Both fields should not allow to fill the past date and time. It only future date and time needs to allow.

 

Can anyone help to write the script

1 ACCEPTED SOLUTION

Tai Vu
Kilo Patron
Kilo Patron

Hi @PRAGHATIESH S 

Have you tried to define an UI Policy to validate if the selected date before today, then clear the date and display the error message. Sample below

Timi_0-1730713209257.png

 

Timi_2-1730713233870.png

Cheers,

Tai Vu

View solution in original post

3 REPLIES 3

jillellaK
Tera Contributor

In your onChange client script you could you the below code 

g_form.setValue("date_field","");
g_form.showFieldMsg("date_field","Enter valid date ","error");

Robbie
Kilo Patron
Kilo Patron

Hi @PRAGHATIESH S,

 

Whilst you can create a client script for this, it can be implemented a lot easier and faster using a Catalog UI Policy.

Here's a similar post and response I provided to another community member not so long ago. Take a look. (Please note, in the example I made it 24hrs in the future, you can simply make it 12 hour or 1 min in the future from the current time depending on your requirement).

 

Check the Response from Robbie post detailing how to implement the UI Policy: https://www.servicenow.com/community/developer-forum/date-variable-cannot-accept-past-date-and-needs...

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie

Tai Vu
Kilo Patron
Kilo Patron

Hi @PRAGHATIESH S 

Have you tried to define an UI Policy to validate if the selected date before today, then clear the date and display the error message. Sample below

Timi_0-1730713209257.png

 

Timi_2-1730713233870.png

Cheers,

Tai Vu