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

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


Hi Cory, I got this working thanks to your help.



I had to add an extra line to create a new js Date object otherwise the form would not submit.




var udate = getDateFromFormat(dateVal, g_user_date_format);  


  var userDate = new Date(udate);


  var day = userDate.getDay();


billi_lumley
ServiceNow Employee
ServiceNow Employee

I tried using the following to prevent a user from selecting Saturday or Sunday and nothing happens when submitting



function onSubmit() {



  var udate = getDateFromFormat('move_date', g_user_date_format);


  var userDate = new Date(udate);


  var day = userDate.getDay();


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


  return false;



  }


}


Hi Billi,



Two things:



1. I suggest opening a new Question on the community, so you can get answers tailored to your specific question.


2. You are passing in the string 'move_date' to the getDateFromFormat function. You want to pass in an actual date value. I assume 'move_date' is a field name- in that case, you'd call getDateFromFormat(g_form.getValue('move_date'), g_user_date_format);



If that doesn't help, please open a new Question with the details of your issue.



Thanks,


Cory