Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

asjfsa.png



Please validate with above screenshot.



Thanks,


Harshvardhan


try this one,



testing: function(){


var abc = new GlideDateTime();


abc.setValue(startDT);


abc.daysAgo(-14);


if (startDT>abc){


return false;


else{


return true;


}


},


gsetNowDateTime: function(){


return gs.nowDatetime();


},


Still showing errors:



find_real_file.png


Was able to get rid of most errors:



var AbstractAjaxProcessorDate = Class.create();
AbstractAjaxProcessorDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {


      type: 'AbstractAjaxProcessorDate'
}
                               
testing: function(){
var abc = new GlideDateTime();
abc.setValue(startDT);
abc.daysAgo(-14);
if (startDT>abc){
return false;
else{
return true;
}
},
gsetNowDateTime: function(){
return gs.nowDatetime();


},




Except for this:



find_real_file.png


you need to remove line4 and as well as return in else causing the errors.



Try this one,


var AbstractAjaxProcessorDate = Class.create();


AbstractAjaxProcessorDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  testing: function(){


  var ans = true;


  var abc = new GlideDateTime();


  abc.setValue(startDT);


  abc.daysAgo(-14);


  if (startDT>abc){


  ans =false;


  }


  return ans;


  },


  gsetNowDateTime: function(){


  return gs.nowDatetime();


  },


  type: 'AbstractAjaxProcessorDate'


});