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

 

13 REPLIES 13

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

Hi @Sagar Pagar 

 I can validate using UI policies and here the main reason is i am learning the script  .Please let me know  what are the changes has to be done to work the script which i have written

I remember reading that.  It works for other than "Relative" date comparisons in a UI Policy.  https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB1919975

Ranjit Nimbalka
Mega Sage

Hi @sushma9 

If u_date_of_reservation is date type filed.

try this script:-

var td = new GlideDate();
var rd = 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 !!!');
}

 

If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.

Regards,

Ranjit Nimbalkar

Hi @Ranjit Nimbalka 

Its not working