Validate past date and current date using client script

sushma9
Tera Contributor

Hi All,

 I am validating the past and current dates  using client scripts. Validation for the past date is working fine but if i take the todays  date its not working properly. Let me know if any changes required in my code. 

Apart from creating script include and on change client scripts and validating with UI Policies  and BR is there other  options to validate  the current date . Please let me know what are the changes to be done in my code to get the expected result.

 

Note : I am new to service now and learning the scripting part

 

Code :

    var td = new Date();
    var rd = new Date(g_form.getValue("u_date_of_reservation"));
    if (td > rd) {
        g_form.addErrorMessage(" You Have Selected the Past Date .Please Select the Future Date For Reservation !!!!")
        g_form.clearValue("u_date_of_reservation");
 
// below part of code is not working  //
    } else if (td = rd) {
        g_form.addInfoMessage(" You Have Selected Todays Date .Please Confirm to create the Reservtaion !!!!")
        return confirm();
    } else{
        g_form.addInfoMessage('Reservation is created successfully !!!');
    }
 
 
Error Message for todays Date :
sushma9_0-1688753354649.png

 

11 REPLIES 11

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

If it's a date/time field, you could also consider using a UI Policy instead of Client Script.

- 2019-08-12 - Article - No Code date validations thru (Catalog) UI Policies

 

Kind regards,
Mark

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

Hi @Mark Roethof 

 I have already gone through  above  articles and i am using them for the catalog UI policies . Let me know if any changes has to be done in my  code to work as expected.

Sagar Pagar
Tera Patron

Hi @sushma9,

I would suggest to use the UI policy instead of Client scripts.

 

You can easily achieve using the UI policies with proper conditions.

Take a look at this article. It will help you.

No Code date validations through (Catalog) UI Policies 

 

Thanks,
Sagar Pagar

The world works with ServiceNow

That's a nice article 🙂

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn