
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2024 11:05 PM
I am trying to do something that I've seen a lot of posts on and I have not been able to get it to work.
I have two variables on a catalog item:
- u_leave_date
- u_return_date
Ultimately I'm just trying to check that the leave date is before the return date and if it isn't, clear the return date value and add a popup message advising the user.
This is the code I have in a catalog client script on the catalog item:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var returndate = g_form.getDisplayValue('u_return_date');
var leavedate = g_form.getDisplayValue('u_leave_date');
var returndateValue = new Date(returndate).valueOf();
var leavedateValue = new Date(leavedate).valueOf();
if (leavedateValue > returndateValue) {
alert("Return date must be after the departure date");
g_form.setValue('u_return_date', '');
}
}
Here is the catalog client script:
It is sort of working - but in reality it is just looking at the first part of the date and as long as the first number is higher in the leave date it will give the message and clear the value, if it is lower it just assumes it is correct.
IE: (date format dd/mm/yyyyy)
Leave date = 04/09/2024
Return date = 03/10/2024
- Because the 03 is before the 04 number wise it will popup advising
Leave date = 04/09/2024
Return date = 28/05/2023
Can someone fix my script please?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2024 12:40 AM
Hey. Try it that way:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var returndate = getDateFromFormat(g_form.getValue('u_return_date'), g_user_date_format);
var leavedate = getDateFromFormat(g_form.getValue('u_leave_date'), g_user_date_format);
var returndateValue = new Date(returndate).valueOf();
var leavedateValue = new Date(leavedate).valueOf();
if (leavedateValue > returndateValue) {
alert("Return date must be after the departure date");
g_form.setValue('u_return_date', '');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2024 11:32 PM
I think this is fine
// Parse the dates into JavaScript Date objects
var leavedateValue = new Date(leavedate);
var returndateValue = new Date(returndate);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2024 11:49 PM
So not sure where you mean I should put those? But assumed they would at least replace these lines?
var returndateValue = new Date(returndate).valueOf();
var leavedateValue = new Date(leavedate).valueOf();
So I tried this:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var returndate = g_form.getDisplayValue('u_return_date');
var leavedate = g_form.getDisplayValue('u_leave_date');
// Parse the dates into JavaScript Date objects
var leavedateValue = new Date(leavedate);
var returndateValue = new Date(returndate);
if (leavedateValue > returndateValue) {
alert("Return date must be after the departure date");
g_form.setValue('u_return_date', '');
}
}
Still does the same thing.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2024 12:18 AM - edited 09-04-2024 12:40 AM
Fixing the code
var returndate = g_form.getDisplayValue('u_return_date');
var leavedate = g_form.getDisplayValue('u_leave_date');
var returndateValue = new Date(returndate).valueOf();
var leavedateValue = new Date(leavedate).valueOf();
// Get the values from the form
var leavedate = g_form.getValue('u_leave_date');
var returndate = g_form.getValue('u_return_date');
// Parse the dates into JavaScript Date objects
var leavedateValue = new Date(leavedate);
var returndateValue = new Date(returndate);
Why not add a debug log to your code to see the values?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2024 12:36 AM
Hi @Moedeb
Have you given a try to the UI Policy? We can leverage the Condition Builder with easy configuration to compare date time between 2 variables. I'll check the script meanwhile.
Cheers,
Tai Vu