Need catalog variable date to validate it is after 10 days from current date

inthuja
Tera Contributor

I have created the below catalog client script which will not let the user to select the past date.I actually need to validate if the entered date is after 10 days from current date. In this case i just wanted to add 10 days to the current date in the client script . how can i do that? Please advice.

 

CLIENT SCRIPT :
var reqDate = new Date(newValue);

var today = new Date();
if(reqDate <= today){
alert('Date must not be in the past.');
}

 

1 ACCEPTED SOLUTION

Chavan AP
Kilo Sage

create a new onchnage client script and script include. It is working as expected.

 

onChange:

 

 

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


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


  return;


  }


  var ga = new GlideAjax('ValidateDate');


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


  ga.addParam('sysparm_date',newValue);


  ga.getXML(CallBack);


 


  function CallBack(response) {


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


  if(answer=='true'){


  alert("pls select atleast 48hrs or more   from now");


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


  }


  }


}

Script include:

var ValidateDate = Class.create();


ValidateDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  dateValidation: function(){


  return (gs.dateDiff(gs.nowDateTime(),new GlideDateTime(this.getParameter('sysparm_date')), true)/3600<48);//change the value according to ur requiremet


  },


  type: 'ValidateDate'


});

 

Glad I could help! If this solved your issue, please mark it as Helpful and Accept as Solution so others can benefit too.*****Chavan A.P. | Technical Architect | Certified Professional*****

View solution in original post

4 REPLIES 4

Brian Lancaster
Tera Sage

You may want to take a look at this blog.  Client side scripting for dates is not reliable so it is better to use a script include and do it via service side scripting.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

use catalog ui policy and there won't be any script required

script below in execute if true condition

function onCondition() {
alert('Date must not be in the past');
g_form.clearValue('start_date');

}

screenshot below

1) 

find_real_file.png

2) 

find_real_file.png

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Chavan AP
Kilo Sage

create a new onchnage client script and script include. It is working as expected.

 

onChange:

 

 

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


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


  return;


  }


  var ga = new GlideAjax('ValidateDate');


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


  ga.addParam('sysparm_date',newValue);


  ga.getXML(CallBack);


 


  function CallBack(response) {


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


  if(answer=='true'){


  alert("pls select atleast 48hrs or more   from now");


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


  }


  }


}

Script include:

var ValidateDate = Class.create();


ValidateDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  dateValidation: function(){


  return (gs.dateDiff(gs.nowDateTime(),new GlideDateTime(this.getParameter('sysparm_date')), true)/3600<48);//change the value according to ur requiremet


  },


  type: 'ValidateDate'


});

 

Glad I could help! If this solved your issue, please mark it as Helpful and Accept as Solution so others can benefit too.*****Chavan A.P. | Technical Architect | Certified Professional*****

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Did you try the catalog ui policy approach?

It does not require any scripting such as onchange script, GlideAjax, Script Include etc

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader