Validate Past date selection on a field

vartika
Tera Contributor

Hi All,

I have a requirement to validate a catalog variable, not to allow user to select any past date. Someone please tell me the working code of it using Client script and Script Include. 

Thanks in Advance!

7 REPLIES 7

Hello @vartika  

 

In my opinion, it's always a best practice to avoid custom scripting when there's a no-code solution available, as it reduces complexity and customization overhead. So, I would recommend going with the no-code option.

That said, I noticed you've achieved this with a UI policy. I've edited my response and included some images for reference. Feel free to review them, and let me know if you're still considering using a client script then you can refer to this thread or else I'm providing the same code from that, so you can refer below as well.

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')); // Replace with 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'); // Replace with your date field
      alert('Business need date should not be in the past.');
    }
  }
}

 

Eshwar Reddy
Kilo Sage

HI @vartika  
If you want to do it via client 
please refer to the below code

On change client script


if (newValue) {
var ga = new GlideAjax('DateValidationUtil');
ga.addParam('sysparm_name', 'isFutureDate');
ga.addParam('sysparm_date', newValue);

ga.getXMLAnswer(function(response) {
if (response === 'false') {
g_form.showFieldMsg("error");
g_form.setValue( ); // Clear the field if the date is in the past
}

}

}

Script Include

isFutureDate: function() {
var dateString = this.getParameter('sysparm_date');
var selectedDate = new GlideDateTime(dateString);
var currentDate = new GlideDateTime();

currentDate.setDisplayValue(currentDate.getDate() + " 00:00:00");

return selectedDate.getTime() > currentDate.getTime() ? 'true' : 'false';
},

Please mark this response as Correct and Helpful if it helps you can mark more that one reply as accepted solution

Thanks
Eshwar




Amit Verma
Kilo Patron
Kilo Patron

Hi @vartika  

 

If you prefer to do it with an On-Change Catalog Client Script and Script Include, you can refer below posts :

https://www.servicenow.com/community/developer-forum/date-field-should-not-take-past-date/m-p/253932...

https://www.servicenow.com/community/developer-forum/how-to-restrict-past-date-selection/m-p/1437062

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.