How to restrict past date selection.

devr
Kilo Contributor

Hi All,

I found below details from one of the thread to restrict past date selection in catalog item however the same is not working in Normal table form, please suggest how to achieve the same in normal table forms (reference- https://community.servicenow.com/thread/159148😞

____________________________________________________________________________

OnChange Client Script : field name : date_time
Script :
function onChange(control, oldValue, newValue, isLoading)
{
if(isLoading){ return; }
if(newValue != ''){
var ga = new GlideAjax('CheckDate');
ga.addParam('sysparm_name', 'chkCatDate');
ga.addParam('sysparm_date',g_form.getValue('date_time'));
ga.getXML(DatParse);
}
function DatParse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'false'){
alert("Date cannot exist in past.");
g_form.setValue('date_time', ''); // Empty the variable.
}}}

___________________________________________________________________________

Now write a script include as follows :-

Name : CheckDate
Client callable : true
Script :
var CheckDate = Class.create();
CheckDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
chkCatDate : function() {

var start = this.getParameter('sysparm_date');
var currDay = gs.now();
if(start < currDay){
return false;
}
else
{ return true; } } });

1 ACCEPTED SOLUTION

Prateek kumar
Mega Sage

Hello


Try this and let me know if i can help you with anything.


Script Include:


Name:DateValidation


Client callable: Checked



var DateValidation = Class.create();


DateValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  validateDate: function() {


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


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


  },


      type: 'DateValidation'


});



Client Script:



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


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


              return;


      }


      var ga = new GlideAjax('DateValidation');


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


      ga.addParam('sysparm_end_date',g_form.getValue('u_business_need_by_date'));//give your date field


      ga.getXML(ProcessResult);


     


     


      function ProcessResult(response) {


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


              if (answer < 0)


                      {


                      g_form.clearValue('u_business_need_by_date'); //give your date field


                      alert('Business need date should not be in the Past.');


                     


              }


      }


}



Please mark my response as correct and helpful if it helped solved your question.
-Thanks

View solution in original post

12 REPLIES 12

Now I understand. Thanks for clarifying.

Hi ,

 

I am getting below message

 

Rajesh77_0-1686308410215.png

 

Shuha2
Kilo Contributor

Hi, I am facing the same issue and have come across this post. I have tried the above script for my scoped app however it does not work, I am fairly new to dev...is anyone able to help please. 

Below is my client script and script include:

 

 

 

Thank you