- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2017 09:37 AM
I need an 'OnChange' script that will check to see whether the entered 'start_date' that is less than 2 weeks from today (or whatever day they are filling out the form). If they enter a date less than 2 weeks away, I want to display a pop up message and prevent them from submitting the form.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2017 05:22 AM
Hi Nicole,
If you don't want to do ajax calls each time, you can use JavaScript validation in onChange client script and onSubmit script :
var date1 = new Date(g_form.getValue('start_date');
var date2 = new Date();
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
if(diffDays >= 14) {
alert("Error Message");
return false;
}
else
return true;
Regards,
Chirag Bagdai
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2017 07:31 AM
Very valid point Dave.
In that case, you can use Display Business Rule to get date in user's timezone and set the result in g_scratchpad as describe below :
var gdt = new GlideDateTime(gs.nowDateTime());
gdt.addDays(-14); // two week ago
- g_scratchpad.current_date_user_timezone = gdt.getDate().toString();
gs.nowDateTime() function will give you current date/time in user's timezone.
and your client script should work to just compare the date because we have already calculated 2 week ago date in business rule :
var date1 = new Date(g_scratchpad.current_date_user_timezone);
var date2 = new Date(g_form.getValue('start_date'));
if(date1 > date2) {
alert("Error Message");
return false;
}
else
return true;
I hope this helps.
Please try and let me know in-case of any question.
Regards,
Chirag
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2017 07:57 AM
This is not working. No message displays regardless of date selected. Business Rule:
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var date1 = new Date(g_scratchpad.current_date_user_timezone);
var date2 = new Date(g_form.getValue('start_date'));
if(date1 > date1) {
g_form.clearValue('start_date');
alert("You've entered a start date less than two weeks prior to the Start Date of the course. We cannot support all of the logistical requirements to ensure a successful delivery for this course.");
return false;
}
else
return true;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2017 08:02 AM
My Bad,
Can you please update the if condition "if(date1 > date1) " with if(date1 > date2) and check if that works.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2017 08:12 AM
Unfortunately, no. Still no message displayed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2017 02:26 PM
Hi Nicole,
I just tried and found the issue, you need to update in display business rule with following script (line# 3)
var gdt = new GlideDateTime(gs.nowDateTime());
gdt.addDays(-14); // two week ago
g_scratchpad.current_date_user_timezone = gdt.getDate().toString();
Please try again and let me know if that works.
Regards,
Chirag