- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-20-2024 09:27 AM - edited ‎05-20-2024 09:51 AM
I'm trying to create an onChange Catalog Client Script that will display a modal pop up when someone enters a date that is within 30 days from the current date. I've tried using a number of scripts posted on community but they are not working for me. We do not want to use a UI Policy/Action for this. Any help with a script to achieve this would be greatly appreciated!
- Variable = u_correction_begin_date
- If u_correction_begin_date is within 30 days of current date they should receive a modal pop up indicating "you cannot submit a date within 30 days of today."
- If u_correction_begin_date is 30 days or more, no pop up should appear.
Thank you in advanced!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-20-2024 10:29 AM - edited ‎05-20-2024 10:30 AM
Hi @Valerie24 ,
Create an onchange client script and try the below code
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var currentDate = new Date();
var selectedDate = new Date(g_form.getValue('u_correction_begin_date'));
// Calculate the difference in days
var timeDiff = selectedDate - currentDate;
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); // Convert milliseconds to days
// Check if the selected date is within 30 days of the current date
if (parseInt(diffDays)<30 && parseInt(diffDays)>=0) {
alert('Please select a date that is at least 30 days from today.');
g_form.clearValue('u_correction_begin_date'); // Optionally clear the field
}
}
Try this and let me know if it works
Regards
Vengadesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-20-2024 10:29 AM - edited ‎05-20-2024 10:30 AM
Hi @Valerie24 ,
Create an onchange client script and try the below code
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var currentDate = new Date();
var selectedDate = new Date(g_form.getValue('u_correction_begin_date'));
// Calculate the difference in days
var timeDiff = selectedDate - currentDate;
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); // Convert milliseconds to days
// Check if the selected date is within 30 days of the current date
if (parseInt(diffDays)<30 && parseInt(diffDays)>=0) {
alert('Please select a date that is at least 30 days from today.');
g_form.clearValue('u_correction_begin_date'); // Optionally clear the field
}
}
Try this and let me know if it works
Regards
Vengadesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-20-2024 11:03 AM
This works perfectly! Thank you so much!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-22-2024 07:59 AM
Is there a way to update the script to also prevent users from entering past dates (i.e. cannot enter 5/21/2024 or 4/25/2024)?
Thank you in advanced!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-22-2024 09:27 AM - edited ‎05-22-2024 09:28 AM
Hi @Valerie24 ,
You can configure a onChange Client script on your date field which has to be validated and give the script given below
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var currentDate = new Date();
currentDate.setHours(0, 0, 0, 0); // Set time to 00:00:00 to compare only dates
// Get the value of the date field (replace 'your_date_field' with the actual field name)
var dateField = newValue;
var enteredDate = new Date(dateField);
// Compare the dates
if (enteredDate<currentDate) {
g_form.clearValue("Your field name");
alert('You cannot enter a past date.');
}
}
Give the name of your field as the parameter inside clearValue() method.
If you find this helpful, mark it as helpful.
Thank you!