
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-14-2016 11:09 AM
I have a need to make sure that the duration between the Begin Date and End Date does not exceed 1 year.
I used scripting before to make a date go out to xxx amount of days, but never to actually do a calculation between Begin and End dates
I don't have a Due Date field. I just have the Start and End Date field and I need to make sure that Duration cannot exceed 1 year.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-14-2016 11:22 AM
Hello Karen, its been a while hope you are well!
Here is some example code I have used before. Do you want the validation to happen on submit or as the user is inputting the data? My code below is based on an onChange of the Work End date on a task, but you could change it to an onSubmit can calculate and warn then.
Create a onChange client script for the End date:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var starts = g_form.getValue('work_start');
startMillis = getDateFromFormat(starts, g_user_date_time_format);
var ends = newValue;
endMillis = getDateFromFormat(ends, g_user_date_time_format);
var minutes = (endMillis - startMillis) / (1000 * 60);
if(minutes > 480) {
alert('An Activity may only last 8 hours. You must enter multiple activities if the time required exceeds 8 hours.');
g_form.setValue('work_end', '');
}
}
Obviously you will need to calculate the a year to then compare for your alert. Thinking out loud an onSubmit may be better because you can prevent the save if it exceeds a year.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-16-2016 08:28 AM
Hi Michael,
The code is working as intended. I have another small question. Now as part of the requirement they want to ensure that the characters are numeric only. I've done this for an onChange script by using the following code. But how can I incorporate this into the script that you provided.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var num = new RegExp("^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$");
if(!num.test(newValue)){
alert('Please enter numbers only in this field');
control.value = oldValue;
}
}
Thanks,
Karen
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-16-2016 08:33 AM
Karen, which field are only numeric characters expected? Is this for the same end date field or another field?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-16-2016 08:50 AM
Yes it is.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-16-2016 09:35 AM
Here you go.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
if(/\D/g.test(newValue)){
alert('Please enter numbers only in this field');
control.value = oldValue;
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-16-2016 09:43 AM
Thank you Abhinay.
The issue is a little more than that. I have an onSubmit client script that will check to ensure that the duration between Begin and end date does not exceed 1 year, the max_length of that field can only be 11 character (not more or less) and now they want to ensure that the characters are only numeric. Here is the onSubmit script that was provided by Michael. If I do the onChange script, along with the onSubmit script they will collide.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var starts = g_form.getValue('work_start');
startMillis = getDateFromFormat(starts, g_user_date_time_format);
var ends = newValue;
endMillis = getDateFromFormat(ends, g_user_date_time_format);
var minutes = (endMillis - startMillis) / (1000 * 60);
if(minutes > 480) {
alert('An Activity may only last 8 hours. You must enter multiple activities if the time required exceeds 8 hours.');
g_form.setValue('work_end', '');
}
}