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

Geoffrey2
ServiceNow Employee
ServiceNow Employee

Your main issue in the undeclared variable ends where newValue should be., and the missing field name in g_form.showFieldMsg().


There is also a gs.dateDiff() function which is useful for comparing dates.



if (isLoading)


      return;


g_form.hideFieldMsg('date',true);


if (newValue == '')


      return;



var ga = new GlideAjax('CheckDateAjax');


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


ga.addParam('sysparm_datefield', newValue);


ga.getXMLAnswer(function(answer) {


      if (Number(answer) > 0) {


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


              return false;


      }


});




var CheckDateAjax = Class.create();


CheckDateAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  compareDateToNow: function () {


          var date = new GlideDateTime();


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


          var diff = gs.dateDiff(date.getDisplayValue(), gs.nowDateTime(), true); // returns seconds


          return diff;


  }


});


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;


  }


}  


Thanks Andrew - this is much cleaner



I amended it slightly and now the client script works as anticipated.



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) {


  alert('Please select a date in futurre.');


  g_form.setValue('ends', '');//clears the date field


return false;


}


}


You may also want to create an onSubmit script that is similar. Otherwise a date might be valid, but then move to invalid the next time the user opens the form.



Also you probably want a condition so that the script is only run in certain states of the record. Assuming this is change then it should only run before approval and the field gets locked down.