Client Script Date Validation Issue

Katie A
Mega Guru

I have a client script to validate that the date selected is on a weekday.

Catalog Client Script onSubmit

  var checkStartDayofWk = getDayofWk(start);

  if (checkStartDayofWk == false) {

  g_form.showFieldMsg('x_myscp_myapp_start_date', 'Selected dates must be on a weekday.','error');

  g_form.submitted = false;

  return false;

  }

  var userDate = new Date(dateVal);

  var userDay = userDate.getDay();

  if (userDay == 0 || userDay == 6) {

  return false;


However, there are random weekdays on the calendar that are returning the incorrect integer value.

0 should be Sunday and 6 should be Saturday.

However, I am seeing that Monday, November 23, 2015 is returning as 0 on the calendar. Saturday, November 21 is returning as a 5.

This is very inconsistent, some dates are correct but others are incorrect.

I ***suspect*** that maybe this issue is since the logic is executed on the CLIENT side rather than the SERVER.

I typically use a Script Include to run the validation on the server and return the value to the client to stop the form on submit.

However, getXMLWait is not available in scoped applications, therefore I cannot use script includes for validation.Access to getXMLWait is not available in scoped applications , what is workaround ?


Let me know if anyone has any suggestions to implement this on the client side to use the onsubmit to stop the submission.

1 ACCEPTED SOLUTION

coryseering
ServiceNow Employee
ServiceNow Employee

Hi Kathryn,



I assume dateVal is the value of the Date/Time field?


You should use getDateFromFormat and the javascript variable "g_user_date_time_format" to get the date as a JavaScript object, adjusted for the user's date format.



var userDate = getDateFromFormat(dateVal, g_user_date_time_format);


var userDay = userDate.getDay()



Why?


https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse#ECMAScri...



The date is likely in the format "2015-09-27 15:33:09" or similar. When passed into the JavaScript Date object, it assumes that is UTC time, so you can end up with off-by-one errors like you are seeing. The client-side "getDateFromFormat" function creates a Date object then adjusts for this peculiarity using the user's preferred date format (since that is what will be in the value).



Thanks to @mallen_nspi for the tip on those functions:


Re: Date validation



I hope that helps,


Cory


View solution in original post

9 REPLIES 9

Thanks, I've posted a new question.  


Does this work for just a date field?   (Not a date/time field)



I have a client script that calculates the difference between the start and end dates to return the number of days.   (Additional fields will appear depending on how many days the calculation spits out - For example, if it is a 3 day difference, then 3 fields appear.   If it is a 2 day difference, 2 fields appear.)   However, this script does not work correctly for users in Europe because they set their formats to dd/MM/yyyy instead of the system default of yyyy-MM-dd.



I haven't been successful in getting this to work for my date fields, so any help would be appreciated.



Thanks


You should be able to use getDateFromFormat to validate dates in any of the date formats in the system.




var udate = getDateFromFormat(dateVal, g_user_date_format);


I keep receiving this error message with the date fields:  



onChange script error: TypeError: Cannot read property 'split' of undefined function (){var o=i(m,arguments);return l.apply(n,o)}



This does not appear when I try it with date/time fields.


Katie A
Mega Guru

I would actually suggest that you post your own question with your code in the Scripting section. I usually have responses to my questions immediately when I post there.