Skip to main content
Participant
March 4, 2023
Question

Time Validation script in pdf form

  • March 4, 2023
  • 2 replies
  • 2714 views

Hi,

 

I have a form with 4 times in it "OUT" "OFF" "ON" and "IN". These times are always greater than the last one. So OFF is always greater than OUT, ON is always greater then OFF and IN is always greater than ON. I am trying to right a script to validate this so the user cannot enter a time in the past. Also how will this work then the times cross midnight. For instance if the ON time is 23:50 and then the in time is 00:10, value wise 00:10 is less than 23:50 but time wise 00:10 is greater.

 

thanks,

eric

This topic has been closed for replies.

2 replies

Thom Parker
Community Expert
Community Expert
March 4, 2023

The best way to check is one time is greater than another is convert it to a date object, filling in the day, month, and Year with some arbitray value. 

Here's a script for 2 dates

// Get date string 
var strTime1 = this.getField("Time1");
var strTime2 = this.getField("Time2");

// Ensure they are both valid time strings, and convert to a date object
var rg24hrTime = /^\s*(\d{1,2})\:(\d{1,2})\s*/;
var oTime1 = null, oTime2 = null;
if(rg24hrTime.test(strTime1))
    oTime1 = new Date(2000,1,1,RegExp.$1,RegExp.$2);
if(rg24hrTime.test(strTime2))
    oTime2 = new Date(2000,1,1,RegExp.$1,RegExp.$2);

// Make sure both are valid times before testing
if((oTime1 != null) && (oTime2 != null)){
   //check direction of time difference 
   if(oTime1 > oTime2)
      app.alert("Time 2 must be less than Time 1");
}

 

But your problem is more complicated.   You need to check 4 dates. Before writing the code you need to figure out a couple things. For example, does the code check if one or more of the times is not filled in? What if they are filled in out of order? is this allowed?

And the really big question, how is the code going to decide if a time crosses midnight? or if it is earlier than the previous time? I would make the users enter the Date as well as time, so there is a explict indicater of the date for a specific time. 

 

Once you know these answers the code above can be employed to test the times.  

 

 

 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
March 4, 2023

> Also how will this work then the times cross midnight. 

It won't. You need to either ask for the full date, not just the hour, or set a limit on the length of each section.

try67
Community Expert
Community Expert
March 4, 2023

Or you need to force the user to not split the time period into two parts, one ending with midnight and the other starting with it, so each part is limited to a single calendar day.