• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit Search
0

Time Validation script in pdf form

New Here ,
Mar 04, 2023 Mar 04, 2023

Copy link to clipboard

Copied

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

TOPICS
JavaScript , PDF forms

Views

1.7K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2023 Mar 04, 2023

Copy link to clipboard

Copied

> 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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2023 Mar 04, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 04, 2023 Mar 04, 2023

Copy link to clipboard

Copied

what do you mean by set a limit on the lenght of each section? I do ask for the date but it is a seperate space on the form. If I ask for date and time then it will disply both and I only want the time displayed. Is there a way to ask for date and time and only display the time? I am trying to make it as easy and functional as possible for the user.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2023 Mar 04, 2023

Copy link to clipboard

Copied

You need to ask for the date of both the start time and the end time. Or set a limit to the length of the period.

Here's an example:

Start Time: 21:00

End Time: 22:00

Is the length of this period 1 hour, or 25 hours? You have no way of knowing, unless you limit the length of each period to 24 hours max. In that case you can determine it's 1 hour.

Another example:

Start Time: 07:00

End Time: 06:00

Is this an error, or did the person start at 7 in the morning of one day, and finished at 6 in the morning of the next day? If you limit each period to 12 hours max, for example, that won't be allowed, and you report it as an error.

None of this will be needed if you told them to specify the dates for each time, though:

Start Time: 21:00 04-Mar-2023

End Time: 22:00 04-Mar-2023

This is Fine.

Start Time: 21:00 04-Mar-2023

End Time: 22:00 03-Mar-2023

Error.

Start Time: 07:00 04-Mar-2023

End Time: 06:00 05-Mar-2023

Fine.

Start Time: 07:00 04-Mar-2023

End Time: 06:00 04-Mar-2023

Error.

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 04, 2023 Mar 04, 2023

Copy link to clipboard

Copied

Ok I see now. How do I set that limit. I could set a 8 or 10 hour max limit and then that would solve the probelm when it crosses midnight right?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2023 Mar 04, 2023

Copy link to clipboard

Copied

LATEST

This is something you would do in your code. You need to convert both values to Date objects.

Then if the second one is before the first one, add a day to it.
Then compare the difference between them. If it exceeds the threshold you set up, report the error, and maybe clear the field(s).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2023 Mar 04, 2023

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines