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

Deepak Kumar5
Kilo Sage

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


var date2 = new Date();


if(date1 <date2){


alert("Add future day");


}


Concise and succinct


A note - missing a )   on the first line.



harel


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

 

var date2 = new Date();

 

if(date1 <date2){

 

alert("Add future day");

 

}

return false;

oharel
Kilo Sage

It should work, but if not, try this (working for me on both catalog item and on normal tables and forms.


u_time1 is the field I am checking.


Client script:


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


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


          return;


  }



  //Type appropriate comment here, and begin script below


      var ga = new GlideAjax("NowDateCalc");


ga.addParam("sysparm_name", "getNow");


ga.getXML(getDate);


}



function getDate(response) {


var my_date = g_form.getValue('u_time1');


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



if (my_date < rightNow) {


alert("Date needed by cannot be earlier than today.");


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


}


}


Script include:


Name: NowDateCalc


Client callable: yes


var NowDateCalc = Class.create();


NowDateCalc.prototype = Object.extendsObject(AbstractAjaxProcessor,{



getNow : function() {


return gs.now();



},


      type: 'NowDateCalc'


});



harel