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

Very valid point Dave.



In that case, you can use Display Business Rule to get date in user's timezone and set the result in g_scratchpad as describe below :



var gdt = new GlideDateTime(gs.nowDateTime());


gdt.addDays(-14); // two week ago


  1. g_scratchpad.current_date_user_timezone = gdt.getDate().toString();  


gs.nowDateTime() function will give you current date/time in user's timezone.



and your client script should work to just compare the date because we have already calculated 2 week ago date in business rule :



var date1 = new Date(g_scratchpad.current_date_user_timezone);  


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


if(date1 > date2) {  


      alert("Error Message");  


      return false;  


}  


else  


        return true;



I hope this helps.



Please try and let me know in-case of any question.



Regards,


Chirag


This is not working.   No message displays regardless of date selected.   Business Rule:


find_real_file.png



Client Script:



function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
          return;
   
}


    //Type appropriate comment here, and begin script below

var date1 = new Date(g_scratchpad.current_date_user_timezone);  
var date2 = new Date(g_form.getValue('start_date'));
if(date1 > date1) {  


g_form.clearValue('start_date');
      alert("You've entered a start date less than two weeks prior to the Start Date of the course. We cannot support all of the logistical requirements to ensure a successful delivery for this course.");
      return false;
}
else
        return true;


 
}


My Bad,



Can you please update the if condition "if(date1 > date1) " with if(date1 > date2)   and check if that works.


Unfortunately, no.   Still no message displayed.


Hi Nicole,



I just tried and found the issue, you need to update in display business rule with following script (line# 3)



var gdt = new GlideDateTime(gs.nowDateTime());


gdt.addDays(-14); // two week ago


g_scratchpad.current_date_user_timezone = gdt.getDate().toString();



Please try again and let me know if that works.



Regards,


Chirag