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

This one worked without errors.   However, the message is still appearing even if I select a date more than 2 weeks out.   Here is the client script:



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.");
  }
  }
}


I thought maybe I needed to change the GlideAjax line to the name of my script include: AbstractAjaxProcessorDate



But that did not resolve the issue.


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


Worked like a charm!   Thank you.


Would that work in all circumstances?



Given that the date field is displayed according to the user's preferences and locale, trying to convert that text to date without knowing the display format could introduce errors.