
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2023 05:49 PM
I made a client script of type onChange for a variable of type date, in which the user can only insert current date or a date after today's date. If the user enters a date before the current day, an error message will be displayed and the field will be cleared.
ClientScript:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var selectedDateNum = getDateFromFormat(newValue, g_user_date_time_format);
var today_date = new Date();
var today_dateStr = formatDate(today_date, g_user_date_time_format);
var todayDateNum = getDateFromFormat(today_dateStr, g_user_date_time_format);
if (selectedDateNum < todayDateNum) {
g_form.showErrorBox("expiration_date", "Expected date cannot be less than today's date", true);
g_form.clearValue('expiration_date');
} else {
g_form.hideFieldMsg('expiration_date', true);
}
}
The script shows the error when inserting a date before the current one and clearing the field value (as it should be), but it doesn't work choosing future or current dates because it clears the field value anyway. What could be wrong?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2023 06:30 PM
I generally prefer using a Client Callable Script Include with GlideAjax to better handle date comparisons in SN.
And Yes, it will take a server round trip.
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2023 06:11 PM
Can you try this script.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var selectedDateNum = getDateFromFormat(newValue, g_user_date_time_format);
var selected_date = new Date(selectedDateNum);
var today_date = new Date();
if (selected_date < today_date) {
g_form.showErrorBox("expiration_date", "Expected date cannot be less than today's date", true);
g_form.clearValue('expiration_date');
} else {
g_form.hideFieldMsg('expiration_date', true);
}
}
Anvesh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2023 06:23 PM
Did not work. And it is no longer throwing the error.
I checked the variable settings and there is no default value or configuration that prevents the client script from working.
What else could cause this client script not to work? because in my view your code seems simple and correct, it should work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2023 06:30 PM
I generally prefer using a Client Callable Script Include with GlideAjax to better handle date comparisons in SN.
And Yes, it will take a server round trip.
Anvesh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2023 06:32 PM - edited 06-23-2023 06:34 PM
Thanks for the support and quick responses @AnveshKumar M