Validate Date in Client Script

CharlesR1
Kilo Guru

Hello All,

Hopefully a quick one - I'm having trouble getting a Client Script to work properly - I have a date field 'ends' (type is 'Date'), which we need to ensure is in the future.

I am using the following Client Script and Script include, but I receive an onChange script error. Any suggestions would be greatly appreciated.

Thanks,

Charles

Client Script:

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

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

          return;

    }

      // assuming datefield is a string

      var ajax = new GlideAjax('CheckDateAjax');

      ajax.addParam('sysparm_name','compareDateToNow');

      ajax.addParam('sysparm_datefield', ends);

      ajax.getXML(SetValues);

               

      function SetValues(response) {

          var answer = response.responseXML.documentElement.getAttribute("answer") * 1;

              if(answer > 0){

                      g_form.showFieldMsg('Please ensure that the Expiry date is in the future!','error');

return false;

              }

     

      }

 

}

Script Include:

      var CheckDateAjax = Class.create();

      CheckDateAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

              compareDateToNow: function () {

              var date1 = new GlideDateTime();

              var date2 = new GlideDateTime();

              date2.setDisplayValue(this.getParameter('sysparm_datefield'));

                      return date1.compareTo(date2);

              }

      });

1 ACCEPTED SOLUTION

andrew_venables
ServiceNow Employee
ServiceNow Employee

No need to use the server one for this, try this in your client script:



g_user_date_format is a global variable that gives you the user's date format


and formatDate and getDateFromFormat are two useful functions for working with dates on the client



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


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


          return;


    }


  //current date  


  var currentDateObj = new Date();  


  var currentDateStr = formatDate(currentDateObj, g_user_date_format);  


  var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_format);  


 


  var startDateNum = getDateFromFormat(newValue, g_user_date_format);  


                 


      if (startDateNum <= currentDateNum) {  


      g_form.showFieldMsg('Please ensure that the Expiry date is in the future!', 'error');


  return false;


  }


}  


View solution in original post

21 REPLIES 21

Community Alums
Not applicable

Do you need this to only work in the Service Portal, or in the platform UI as well?

I would like to have Service Portal and platform UI both respond to the same script. Now, it works for platform UI but not for Service Portal form.

Thank you,

Hello timmo,

If I want to make the Service Portal form to respond the script, what is the fix?

I am not concerned the platform UI now. All scripts work perfectly with the platform UI.

Thank you,

Community Alums
Not applicable

Try this:

if (typeof(moment) === "undefined"){
//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('end_date');
var startDateNum = getDateFromFormat(startDateStr, g_user_date_format);
//get end date
var endDateStr = g_form.getValue('extension_date');
var endDateNum = getDateFromFormat(endDateStr, g_user_date_format);
} else {
// current date
currentDateNum = moment().valueOf();
//get start date
var startDateNum = moment(g_form.getValue('end_date'));
//get end date
var endDateNum = moment(g_form.getValue('extension_date'));
}

This will work in both. It check if the 'moment' library exists (only in SP), and if it does, use that for the date formatting instead of the platform/native UI methods. 

Hope that helps!

ivanovs
Giga Contributor

You can also use formatDate and other date validation scripts in the service portal if you include them as a dependency. See the last post from jacebensen here : https://community.servicenow.com/community?id=community_question&sys_id=c7f20ba1dbd8dbc01dcaf3231f961958

I had the same problem but the link above allowed me to continue using the existing validation scripts.