Date Format Validation in the new Service Portal

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-15-2016 12:49 PM
The fact that ServiceNow does not provide any built-in field validations causes a lot of time consuming extra work in general. The new Service Portal does not support very many built-in functions and this causes even more extra work.
How are we supposed to validate a date format in the ServiceNow Service Portal?
The Date picker accepts mal-formatted dates.
In our CMS I was using the getDateFormat function to ensure that the date is a valid format. This works fine. However, it is to no surprise, not supported in the new Service Portal.
function isValidDate(dateVal) {
var userDateFormat = getDateFromFormat(dateVal, g_user_date_format);
return userDateFormat;
}
So in order to validate a date format, I would need to write several RegExes to manually parse out every possible date format in the system.
Anyone run into this issue?
Servicenow really needs to provide some built-in validations... it would save a lot of tedious extra work.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2018 12:03 PM
Here we go, here is the client-side example of an onSubmit script that I created to validate dates in a form submission.
The method 'isFutureDate' checks the date input and compares it with the datetime from yesterday.
The method 'getYesterdayDateTimeMS()' gets the date time yesterday and uses that to compare it with the user input.
function onSubmit() {
var isValid = checkStartEndDates();
if (isValid == false) {
g_form.submitted = false;
return false;
}
}
function checkStartEndDates() {
g_form.hideAllFieldMsgs('error');
var start = g_form.getValue('u_pickup_date');
var end = g_form.getValue('u_return_date');
var startDateMS = convertStringtoDateMS(start);
var endDateMS = convertStringtoDateMS(end);
var yesterdayDateTime = getYesterdayDateTimeMS();
var checkStartDateFormat = isValidDateFormat(start);
if (checkStartDateFormat == false) {
g_form.showFieldMsg('u_pickup_date', 'Pickup Date is not a valid format. Accepted date format is YYYY-MM-DD.','error');
return false;
}
var checkEndDateFormat = isValidDateFormat(end);
if (checkEndDateFormat == false) {
g_form.showFieldMsg('u_return_date', 'Return Date is not a valid format. Accepted date format is YYYY-MM-DD.','error');
return false;
}
var checkStartDateFuture = isFutureDate(startDateMS, yesterdayDateTime);
if (checkStartDateFuture == false) {
g_form.showFieldMsg('u_pickup_date', 'Selected dates must be in the future.','error');
return false;
}
var checkEndDateFuture = isFutureDate(startDateMS, yesterdayDateTime);
if (checkEndDateFuture == false) {
g_form.showFieldMsg('u_return_date', 'Selected dates must be in the future.','error');
return false;
}
var compareStartEnd = compareDates(startDateMS, endDateMS);
if (compareStartEnd == false) {
g_form.showFieldMsg('u_pickup_date', 'Start date must be before end date.','error');
return false;
}
var compareDaysBetween = getDaysBetween(startDateMS, endDateMS, 21);
if (compareDaysBetween == false) {
g_form.showFieldMsg('u_pickup_date', 'The maximum loan period is 21 days. Please select a shorter duration.','error');
return false;
}
var checkStartDayofWk = getDayofWk(startDateMS);
if (checkStartDayofWk == false) {
g_form.showFieldMsg('u_pickup_date', 'Selected dates must be on a weekday.','error');
return false;
}
var checkEndDayofWk = getDayofWk(endDateMS);
if (checkEndDayofWk == false) {
g_form.showFieldMsg('u_return_date', 'Selected dates must be on a weekday.','error');
return false;
}
}
//Date functions
function isValidDateFormat(dateVal) {
//Check the date against a regex to validate format YYYY-MM-DD
if (!/^([0-9]{4})(\-)([0-9]{2})(\-)([0-9]{2})$/.test(dateVal))
{
return false;
}
//Take input string split by the dash delimiter and convert to array
var format = dateVal.split("-");
//Parse the array and convert each part to integer
var month = parseInt(format[1], 10);
var year = parseInt(format[0], 10);
var day = parseInt(format[2], 10);
//Store the number of days in each month in an array
var monthDays = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
//Check the number of months
if(month == 0 || month > 12)
{
return false;
}
//Check year
if(year < 2016 || year > 2050)
{
return false;
}
//Check if the year is a leap year and modify February in monthDays array
if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
{
monthDays[1] = 29;
}
//Check the number of days in each month
if(day < 0 || day > monthDays[month - 1])
{
return false;
}
}
function convertStringtoDateMS(dateString) {
//Convert user date string to JS date object
var userDateValue = new Date(dateString);
//Get epoch value in milliseconds
var userDateValueMS = userDateValue.getTime();
return userDateValueMS;
}
function getYesterdayDateTimeMS(){
//Get current date time
var nowDate = new Date();
//Set now to yesterdays date since may have reservations on same day
nowDate.setDate(nowDate.getDate() + -1);
//Get epoch value in milliseconds
var nowDateMS = nowDate.getTime();
return nowDateMS;
}
function isFutureDate(userDateValueMS, yesterdayDateTime) {
if (userDateValueMS < yesterdayDateTime) {
return false;
}
}
function compareDates(startDateMS, endDateMS) {
if (startDateMS > endDateMS) {
return false;
}
}
function getDaysBetween(startDateMS, endDateMS, days) {
var dateDiffMS = endDateMS - startDateMS;
var dateDiffSecs = dateDiffMS / 1000;
var dateDiffMins = dateDiffSecs / 60;
var dateDiffHrs = dateDiffMins / 60;
var dateDiffDays = dateDiffHrs / 24;
if (dateDiffDays > days) {
return false;
}
}
function getDayofWk(userDateValueMS) {
//Create new JS date object with user selected date value
var userDate = new Date(userDateValueMS);
//Get UTC day
var userDay = userDate.getUTCDay();
//If selected day is 6 Saturday or 0 Sunday stop submission
if (userDay == 6 || userDay == 0) {
return false;
}
}