Nintex Forms – Business Days Validation
Today, I had a very cool question around doing some validation in a Nintex Form, around business days. In short, how do I prevent a user from submitting a form, if the date selected in the form, is less than 30 business days from today.
VIDEO
Now at this stage, there’s no business day calculations in Nintex Forms, so I had to be a little creative. I thought I could use some JavaScript to do the validation but I didn’t want to spend a whole bunch of time building it out from scratch. Back when I was studying to be a development, they ingrained in us the concept of, don’t reinvent the wheel. So I went to Bing (yes I use that haha), and I found a function I could use : http://snipplr.com/view/4086/
I was able to add it to the Nintex Form I was building and wrap a little more JavaScript in there and got it to work with very little effort.
Here’s the entire JavaScript in the form that I have attached at the bottom of this post, in the Downloads section.
function doWork()
{
var myDate = NWF$(‘.cssDateChosen’).find(‘input’);
var dFuture = new Date(“December 31, 2015”);
dFuture = new Date(myDate.val());
var dNow = new Date();
var result = calcBusinessDays(getDateOnly(dNow),getDateOnly(dFuture));
alert(‘Num of Business Day between Future date and now : ‘ + result + ‘ days’);
}
function dateBusinessDayValidation(source, arguments)
{
arguments.IsValid=true;
var myDate = NWF$(‘.cssDateChosen’).find(‘input’);
var dFuture = new Date(myDate.val());
var dNow = new Date();
var result = calcBusinessDays(getDateOnly(dNow),getDateOnly(dFuture));
if(result < 30)
{
arguments.IsValid = false;
}
}
function calcBusinessDays(dDate1, dDate2)
{
// input given as Date objects
var iWeeks, iDateDiff, iAdjust = 0;
if (dDate2 < dDate1)
return -1; // error code if dates transposed
var iWeekday1 = dDate1.getDay(); // day of week
var iWeekday2 = dDate2.getDay(); iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7
iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;
if ((iWeekday1 > 5) && (iWeekday2 > 5))
iAdjust = 1; // adjustment if both days on weekend
iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2; // calculate difference in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
iWeeks = Math.floor((dDate2.getTime() – dDate1.getTime()) / 604800000) ;
if (iWeekday1 <= iWeekday2)
{
iDateDiff = (iWeeks * 5) + (iWeekday2 – iWeekday1);
}
else
{
iDateDiff = ((iWeeks + 1) * 5) – (iWeekday1 – iWeekday2);
}
iDateDiff -= iAdjust; // take into account both days on weekend
return (iDateDiff + 1); // add 1 because dates are inclusive
}
function getDateOnly(myDate)
{
var dd = myDate.getDate();
var mm = myDate.getMonth()+1;
//January is 0!
var yyyy = myDate.getFullYear();
if(dd<10)
{
dd=’0’+dd;
}
if(mm<10)
{
mm=’0’+mm;
}
var newDateOnly = mm+’/’+dd+’/’+yyyy;
return new Date(newDateOnly);
}
The form looks like this :
There are two things you can try here.
1. Select a date and then press the “Get Num Business Days” button. This will do a popup, letting you know how many business days there between now and the date selected (including today).
2. Select a date and press save. This will run the JavaScript Validation function and will prevent you from saving the form, unless the date selected is greater than or equal to 30 business days from now.
Date Control Settings
Open up the Date Control settings and you’ll need to set the CSS class and also enable the Custom Validation, set the JavaScript function you want to call and the Validation Message that you want to appear, should the validation function return invalid.
Downloading a Nintex Forms Trial
If you haven’t seen Nintex Forms, you have no idea what you are missing. Get a download here and install it on a dev SharePoint environment. It takes almost no time to install.
Conclusion
I have not tried this in the Office 365 version of Nintex Forms, but I see no reason why this would not work. There may be some tweaking required, but the fundamental concept should be sound.
If you have any questions, please post them at the bottom of this blog post.
NOTE: I have only tried this on an English SharePoint site. I am not sure if there will be issues with the way dates are being handled/calculated in other parts of the world.
Also, this considers that Saturday and Sunday are not business days. So if you live in a part of the world where this is not the case, you will need to modify the JavaScript.
Downloads
Download the Nintex Form (.xml)– Download, Unzip and import into the Nintex Form Designer page