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

Harsh Vardhan
Giga Patron

Hi Nicole,



Please check the script below.



Script Include:




var test = Class.create();


test.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  testing: function() {


  var startDT = this.getParameter('sysparm_sdt');


  var abc= gs.daysAgo(-14);





  if (startDT > abc) {


  return false;


  } else {


  return true;


  }


  },



  getNowDateTime: function() {


  return gs.nowDateTime();


  }



});






Client Script:



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


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


  return;


  }


  var sd=g_form.getValue('u_start_date');


  alert(sd);



  var startDate = newValue;


  var ga = new GlideAjax('test');


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


  ga.addParam('sysparm_sdt', sd);


  ga.getXML(checkDate);



}




function checkDate(response) {


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


  if (answer == 'false') {


  alert("please write your message ");


g_form.setValue('u_start_date', '');   }



}





Script Include must be client callable. and i just reset the field when ever user will try to select the date more than 14 days on the basis of current date.


you can try with another client side pop up functionality.



Thanks,


Harshvardhan


shloke04
Kilo Patron

Simply comment out the alert statement before your if block i.e.if(answer<14) before this if block there is an alert message in Line Number 19.Remove it from your code or comment it out and this would resolve your issue. Please see the screen shot below:



find_real_file.png



Hope this helps.Mark the answer as correct/helpful based on impact.



Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Thank you, this worked to remove the extra message.   However, the correct message is still displaying, even if I select a date more than 2 weeks from now (even if a full month from now).


have you tried my script?


i tested in my demo instance and it is working fine.



Thanks,


Harshvardhan


Getting several errors in the script include:



find_real_file.png