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

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

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

Ranjit Nimbalka
Mega Sage
 
try this this will work(if u_date_of_reservation type is date only not a date/time ).
var today_date = new Date();
var td = formatDate(today_date, g_user_date_format);
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");
} 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 !!!');
}
//Type appropriate comment here, and begin script below
}
 
 
if it is date/time then you have to split like this (

var dateTimeStr = g_form.getValue('u_date_of_reservation');

var dateArr = dateTimeStr.split(' ');

var rd = dateArr[0];

)

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

Regards,

Ranjit

HI @Ranjit Nimbalka 

Its a date Time field