The CreatorCon Call for Content is officially open! Get started here.

Date Format Validation in the new Service Portal

Katie A
Mega Guru

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.

Screen Shot 2016-09-15 at 3.42.53 PM.png

10 REPLIES 10

I haven't tried it, but I don't see why not. More often we have to worry about scripts not working with the Portal rather than the other way around.


Hello Sunil,



Can you help me how to import JS files to our instance?


Hi Sunil,


i have used the below script for my start/end date validation in my Helsinki . It is working as expected but only thing is that it is not accepting today's date. It gives the same error : Start date cannot be in the past. I assume we need to do some small changes.



Below is my On change Client script for start date:



function onChange(control, oldValue, newValue, isLoading) {


    if (isLoading || newValue == '') {


          return;


    }



var currentDateNum;


var startDateNum;


var endDateNum;


g_form.hideFieldMsg('u_security_start_date');


g_form.hideFieldMsg('u_security_end_date');



if (typeof (moment) === "undefined") {



      //Get start date


      var startDateStr = g_form.getValue('u_security_start_date');


      startDateNum = getDateFromFormat(startDateStr, g_user_date_time_format);



      //Get end date


      var endDateStr = g_form.getValue('u_security_end_date');


      endDateNum = getDateFromFormat(endDateStr, g_user_date_time_format);



//Get current date


      var currentDateObj = new GlideDate();


      var currentDateStr = formatDate(currentDateObj, g_user_date_time_format);


      currentDateNum = getDateFromFormat(currentDateStr, g_user_date_time_format);


}



else


{


// current date


      currentDateNum = moment().valueOf();


      //get start date


      startDateNum = moment(g_form.getValue('u_security_start_date'));


      //get end date


      endDateNum = moment(g_form.getValue('u_security_end_date'));


}



if(newValue == '')


{


return;


}


if(endDateNum!='')


{


if (endDateNum < startDateNum)


{


g_form.hideFieldMsg('u_security_start_date');


    g_form.showFieldMsg('u_security_start_date', 'Start date must be before the End date','error');


    g_form.setValue('u_security_start_date','');


    return false;


}


}



//if start is past date



if (startDateNum < currentDateNum) {


g_form.hideFieldMsg('u_security_start_date');


g_form.showFieldMsg('u_security_start_date', 'The Start Date cannot be in the past','error');


g_form.setValue('u_security_start_date','');


return false;


}



}



It gives the below error for today's date as well.. I want to able to select the current date. Can you pls help???



find_real_file.png





Thanks


Hi Chaitanya,

I am facing the same issue, did you find the solution for this 

 

I believe you might have to compare it to yesterday's date instead of today's date.

I wrote a set of server-side Script Include methods to handle date validation. The checkFutureDate method compares a date input with yesterday's date.

I did not end up using the moment library but I am using getDayOfWeekLocalTime() and other built-in ServiceNow methods. Note that this will only work on the server-side. I may still have a client-side example as well, I will check back and let you know.

https://docs.servicenow.com/bundle/kingston-application-development/page/app-store/dev_portal/API_reference/GlideDateTime/concept/c_GlideDateTimeAPI.html

 

var DateValidationUtil = Class.create();
DateValidationUtil.prototype = {
initialize: function () {},

// Convert date string to milliseconds
convertDatetoMS: function(date_string) {
date_string = date_string.toString();

// Initialite Glide Date Time object
var date_gdt = new GlideDateTime();

// Set Glide Date Time object to date selected by user
date_gdt.setDisplayValue(date_string);

// Get number of milliseconds
var date_ms = date_gdt.getNumericValue();

return date_ms;

},

// Get day of week integer value
getDayofWeek: function(date_string) {
date_string = date_string.toString();

var gdt_input = new GlideDateTime(date_string);
var day_of_week = gdt_input.getDayOfWeekLocalTime();

return day_of_week;

},

// Check if date input is in the future
checkFutureDate: function(date_string) {
date_string = date_string.toString();

// Get date yesterday to allow reservations to be made on same day
var yesterday_date_gdt = new GlideDateTime();
yesterday_date_gdt.addDaysLocalTime(-1);

// Get date input and convert date string to GlideDateTime object
var input_date_gdt = new GlideDateTime(date_string);

/*
* Compare two dates
* compareTo() result values:
* 0 = Dates are equal
* 1 = The object's date is after the date specified in the parameter
* -1 = The object's date is before the date specified in the parameter
*/

// Check if input date is in future by comparing to yesterday date
var date_in_future = input_date_gdt.compareTo(yesterday_date_gdt);


// If date_in_future is 1 input_date_gdt is in the future, return true
// If it is 0 or -1 it is in the past, return false
var date_in_future_result = (parseInt(date_in_future, 10) === 1) ? true : false;

return date_in_future_result;

},

// Check start date before end date
compareStartEnd: function(start_date, end_date) {
start_date = start_date.toString();
end_date = end_date.toString();

// Convert date string to GlideDateTime object
var start_date_gdt = new GlideDateTime(start_date);
var end_date_gdt = new GlideDateTime(end_date);

// Check if start date is before end date
var start_before_end = end_date_gdt.compareTo(start_date_gdt);

// If start_before_end is 0 or 1 the start_date is equal to or before the end_date, return true
// If start_before_end is -1 the start_date is after the end_date, return false
var start_before_end_result = (parseInt(start_before_end, 10) === -1) ? false : true;

return start_before_end_result;
},

// Check if number of days between start_date_ms and end_date_ms exceeds max_days
checkDaysBetween: function(start_date, end_date, max_days) {

start_date = start_date.toString();
end_date = end_date.toString();

var start_date_ms = this.convertDatetoMS(start_date);
var end_date_ms = this.convertDatetoMS(end_date);

var date_diff_ms = end_date_ms - start_date_ms;
var date_diff_secs = date_diff_ms / 1000;
var date_diff_mins = date_diff_secs / 60;
var date_diff_hrs = date_diff_mins / 60;
var date_diff_days = date_diff_hrs / 24;

if (date_diff_days > max_days) {
return false;
} else {
return true;
}
},

type: 'DateValidationUtil'
};