- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2016 08:32 AM
Hi Team,
I did date validation in client side for following scenarios.
We have two fields start date and end date. Two dates are pre-populate today's date. (i.e. Start date: 06-05-2016 and End date: 06-05-2016)
For start date validation:
- User should not select past date.
For this I wrote following Onchange client script:
function onChange(control, oldValue, newValue, isLoading) {
if (!isLoading) {
if(newValue != '') {
//current date
var currentDateObj = new Date();
var currentDateStr = formatDate(currentDateObj, g_user_date_format);
var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_format);
//get start date
var startDateStr = g_form.getValue('u_onboard_date');
var startDateNum = getDateFromFormat(startDateStr, g_user_date_format);
if (startDateNum < currentDateNum) {
alert('You cannot select a date in the past.');
g_form.setValue('u_onboard_date', '');
}
else if (startDateNum < currentDateNum) {
alert('You cannot select a date in the past.');
g_form.setValue('u_onboard_date', '');
}
}
}
}
For End date validation:
- User should not select past date.
- User should not select and end date prior to the start date.
- User should not select more than 365 days from start date.
For this I wrote following Onchange client script:
function onChange(control, oldValue, newValue, isLoading) {
if (!isLoading) {
if(newValue != '') {
//current date
var currentDateObj = new Date();
var currentDateStr = formatDate(currentDateObj, g_user_date_format);
var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_format);
//get start date
var startDateStr = g_form.getValue('u_onboard_date');
var startDateNum = getDateFromFormat(startDateStr, g_user_date_format);
//get end date
var endDateStr = g_form.getValue('u_expected_offboard_date');
var endDateNum = getDateFromFormat(endDateStr, g_user_date_format);
var diff = endDateNum - startDateNum; //alert("end:"+endDateNum); alert("start:"+startDateNum); alert("diff:"+diff);
var maxDiff = 30*24*60*60*1000; //30 days * 24 hrs * 60 mins * 60 secs * 1000 ms
if (endDateNum <= 0){
alert('Please use the calendar icon to select a date.');
g_form.setValue('u_expected_offboard_date','');
}
else if (endDateNum < currentDateNum) {
alert('You cannot select a date in the past.');
g_form.setValue('u_expected_offboard_date','');
}
else if (endDateNum<startDateNum){
alert('You cannot select an end date prior to the start date.');
g_form.setValue('u_expected_offboard_date','');
}
else if (diff>=31536000000){
alert('You cannot select more than 365 days from start date.');
g_form.setValue('u_expected_offboard_date','');
}
}
}
}
But, my requirement is, how can we write date validation for above scenarios in server side. Please provide any useful scripts for the same.
It would be very helpful to me.
Thank you for you great help in advance.
Thanks & Regards,
Prasanna Kumar
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2016 08:46 AM
I think you mean a business rule....right?
I have two date comparisons written in my instance in different way... YOu can try both...
first one:- insert and update business rule
var diff = gs.dateDiff(gs.nowDateTime(),current.start_date.getDisplayValue(),true);
//This will calculate diff between 'change date' field time and Present time.
if (diff < 0)
{
// set error message on "change_date" field
current.start_date.setError('Date entered is from past, please enter date for future');
}
else
{
current.update();
}
second :- before insert and update business rule
verifyDates();
function verifyDates() {
var dReqStart = new GlideDateTime();
dReqStart.setDisplayValue(current.start_date);
var dReqEnd = new GlideDateTime();
dReqEnd.setDisplayValue(current.end_date);
var today = new GlideDateTime();
today.setDisplayValue(gs.nowDateTime());
if ((dReqStart.getNumericValue() - today.getNumericValue())/(1000*60*60*24) < 0) {
gs.addErrorMessage("Cannot Request for Approval! Implementation Start Date and time cannot begin before today.");
current.approval='not requested';
current.state.setValue(6);
current.setAbortAction(true);
} else if ((dReqEnd.getNumericValue() - dReqStart.getNumericValue())/(1000*60*60*24) < 0) {
gs.addErrorMessage("Cannot Request for Approval! Implementation End Date and time cannot come before Request Start Date.");
current.approval='not requested';
current.state.setValue(6);
current.setAbortAction(true);
return false;
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2016 08:46 AM
I think you mean a business rule....right?
I have two date comparisons written in my instance in different way... YOu can try both...
first one:- insert and update business rule
var diff = gs.dateDiff(gs.nowDateTime(),current.start_date.getDisplayValue(),true);
//This will calculate diff between 'change date' field time and Present time.
if (diff < 0)
{
// set error message on "change_date" field
current.start_date.setError('Date entered is from past, please enter date for future');
}
else
{
current.update();
}
second :- before insert and update business rule
verifyDates();
function verifyDates() {
var dReqStart = new GlideDateTime();
dReqStart.setDisplayValue(current.start_date);
var dReqEnd = new GlideDateTime();
dReqEnd.setDisplayValue(current.end_date);
var today = new GlideDateTime();
today.setDisplayValue(gs.nowDateTime());
if ((dReqStart.getNumericValue() - today.getNumericValue())/(1000*60*60*24) < 0) {
gs.addErrorMessage("Cannot Request for Approval! Implementation Start Date and time cannot begin before today.");
current.approval='not requested';
current.state.setValue(6);
current.setAbortAction(true);
} else if ((dReqEnd.getNumericValue() - dReqStart.getNumericValue())/(1000*60*60*24) < 0) {
gs.addErrorMessage("Cannot Request for Approval! Implementation End Date and time cannot come before Request Start Date.");
current.approval='not requested';
current.state.setValue(6);
current.setAbortAction(true);
return false;
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2016 10:51 AM
One note about your first script, Mansi.
Avoid using the current.update() in business rules. If you are manipulating the current object, typically these go in a BEFORE business rule, which has an implicit update.
If you're doing this an AFTER rule, a current.update() operation is going to retrigger all the business rules again (probably not what you want.)
Your business rule has no data changes, so it doesn't need a current.update() anyway. 🙂 Perhaps if the error is found, a setAbortAction() with a BEFORe is more appropriate to stop any updates from happening.
Business Rules Best Practices - ServiceNow Wiki

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2016 12:27 PM
Thanks Chuck. . I will take care of this in future.