Need help with Date Validation script

nicolemccray
Tera Expert

I need an 'OnChange' script that will check to see whether the entered 'start_date' that is less than 2 weeks from today (or whatever day they are filling out the form).   If they enter a date less than 2 weeks away, I want to display a pop up message and prevent them from submitting the form.

1 ACCEPTED SOLUTION

chirag_bagdai
ServiceNow Employee
ServiceNow Employee

Hi Nicole,



If you don't want to do ajax calls each time, you can use JavaScript validation in onChange client script and onSubmit script :



var date1 = new Date(g_form.getValue('start_date');


var date2 = new Date();


var timeDiff = Math.abs(date2.getTime() - date1.getTime());


var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));


if(diffDays >= 14) {


      alert("Error Message");


      return false;


}


else


        return true;



Regards,


Chirag Bagdai


View solution in original post

34 REPLIES 34

h_ctort
Giga Contributor

Hi Nicole,


you need to do a GlideAjax to a script include where you can validate the date with the function gs.daysAgo(-14)) which gets the actual date and adds 14 days, if the field date entered is less than that you use the g_form.addInfoMessage() to pop up the alert and the g_form.setValue to wipe the mistake date.


shloke04
Kilo Patron

Hi,



You can do this with the help of an Script Include and a On Change Client Script as mentioned below:



1) Create a Script Include with below code:



Script:



var DateValidation = Class.create();


DateValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  validateDate3: function() {


  var ActualEndDate = this.getParameter('sysparm_start_date');


  return gs.dateDiff(gs.now(),ActualEndDate, true)/86400;


  },





      type: 'DateValidation'


});




find_real_file.png



2) Now create an On Change Client Script on the required Table and field to check on the same. For example The below script is to check for the Planned Start Date field on the Change Request form:



Script:



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


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


  return;


  }



  var ga = new GlideAjax('DateValidation');


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


  ga.addParam('sysparm_start_date',g_form.getValue('start_date'));


  ga.getXML(ProcessResult);




  function ProcessResult(response) {


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


    alert(answer);


  if (answer < 14)


  {


  g_form.clearValue('start_date');


  alert('Start Date should not be more than 14 Days in the Past.');


  }


  }


}



find_real_file.png



Hope this helps.Mark the answer as correct/helpful based on impact.



Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

This is the script I currently have:



function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
          return;
   
}


    //Type appropriate comment here, and begin script below

var cdt = g_form.getValue('start_date'); //first Date/Time field  
var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.  
 
var ajax = new GlideAjax('ClientDateTimeUtils');  
ajax.addParam('sysparm_name','getDateTimeBeforeNow');  
ajax.addParam('sysparm_fdt', cdt);  
ajax.addParam('sysparm_difftype', dttype);  
ajax.getXML(doSomething);  

function doSomething(response){  
var answer = response.responseXML.documentElement.getAttribute("answer");  
alert(answer);  


if (answer < 14){
 
g_form.clearValue('start_date');
alert("You've entered a start date less than two weeks prior to the Start Date of the course. We cannot support all the logistical requirements to ensure a successful delivery for this course.");
  }
  }
}



And this is what returns in the UI:



find_real_file.png



How can I fix this to return my message?


After I click 'Ok' on the message, the correct one appears with the text.