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

This does not work in Service Portal.



'formatDate' and 'getDateFromFormat' are not available in the Service Portal libraries, I assume because they are not available in mobile. The Moment.JS library is available in SP, though, so that can be used as an alternative to those two functions.  



I just had to update a client script on a form that worked in CMS/Native UI, but not in SP. I kept the original code in place in the event someone invoked the form from within the Native UI.



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'));


}



Shout out to ben.hollifield for his help on the Slack channel!


Tried this on our Record Producer catalog UI policy and it didn't work.

arnabwa
Giga Guru

Hi Charles,



You could use this onChange client script only :



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


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


  return;


  }



var currentDate = new Date();


var currentDateFormat = formatDate(currentDate, g_user_date_time_format); // get the user date format


var currentDateFinal = getDateFromFormat(currentDateFormat, g_user_date_time_format);//convert to the user date format




//get duration


var duration = g_form.getValue('ends');//get value of required date field


var durationFinal = getDateFromFormat(duration, g_user_date_time_format);//convert to the user date format





var difference = durationFinal - currentDateFinal;



if (difference < 0) {


  //flag++;


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


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


return false;


}


}



Thanks,


Arnab


Harneet Sital
Mega Sage
Mega Sage

Hi Charles,



Rather than using a Client Script use the following UI Policy. Works as per your requirement.


Make a UI Policy as follows



Capture.PNG



Write following script :


function onCondition() {


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


      alert("Enter valid date");


}



Let me know if you need more information/help on the same.


Hi Harneet Sital, 

I have solved my 'start date cannot be before today' problem with your solution. However, when user choose the correct date my error message is still there. How can I solve this.

Thanks.