In onChange client script i can't able to get the error message please help me out the reason

kranthi2
Tera Expert

Hi,

In onChange client script i can't able to get the error message please help me out the reason.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var currentDate = new Date();
    var selectedDate = new Date(newValue);

    if (selectedDate > currentDate) {
        g_form.showErrorBox('Invalid Date', 'Future dates are not allowed.');
        g_form.setValue('issue_date', ''); // Replace 'your_date_field' with the actual field name
    }
}
1 ACCEPTED SOLUTION

Community Alums
Not applicable

HI @kranthi2 ,

Please use "  g_form.addErrorMessage('message goes here'); " not g_form.showErrorBox('Invalid Date', 'Future dates are not allowed.');

View solution in original post

4 REPLIES 4

OlaN
Giga Sage
Giga Sage

Hi,

This kind of date comparison will not work, because it's different in the formatting.

the "new Date()" will return something like this: "Mon Dec 11 2023 07:28:47 GMT+0100..."

while the newValue probably will return something like: "2023-01-31 Tue 04:33:54"

 

I would recommend sending the existing date (newValue) to a scriptInclude and do the comparison serverside, then return true/false back from the function, and use the returnvalue from the function in your clientscript.

Community Alums
Not applicable

HI @kranthi2 ,

Please use "  g_form.addErrorMessage('message goes here'); " not g_form.showErrorBox('Invalid Date', 'Future dates are not allowed.');

Anil Lande
Kilo Patron

Hi,

if it is simple validation then you can use UI Policy:

Please check below Article:

https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-...

 

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Harish KM
Kilo Patron
Kilo Patron

Hi @kranthi2  You can achieve this with UI Policy like below

HarishKM_0-1702276648675.png

 

 

HarishKM_1-1702276661808.png

HarishKM_2-1702276675579.png

 

Alternatively you can use the client script below

   var currentDate = new Date().getTime(); // todays date
    var refreshDate = new Date(g_form.getValue('date')).getTime(); // user selected date
    if (refreshDate > currentDate) {
        var message = 'Cannot select Future Date';
       
        g_form.clearValue('date');
        g_form.showErrorBox('date', message);
       
    }

 

Regards
Harish