Skip to main content
Participant
February 16, 2022
Answered

How do I force a Form field into a specific format?

  • February 16, 2022
  • 2 replies
  • 1980 views

I am using Acrobat X. I am creating a chemical batching form. There are multiple fields for different loads. I don't want the fields to be auto populated.  I want allow the user to type 1515 and the field be populated with 3:15 pm. I do not want this to be a document level calculation. I want the calculation or formatting to be done per field. Is this possible?

This topic has been closed for replies.
Correct answer Thom Parker

To do this you need a custom format script.  A format script changes how the data is displayed, it does not change the actual value of the field.

 

if(/^\s*(\d{1,2})(\d{2})\s*$/.test(event.value))
{
    var nHour = Number(RegExp.$1);
    event.value = ((nHour>12)?nHour-12:nHour).toString() + ":" + RegExp.$2 + ((nHour >= 12)?" pm":" am");
}
else
   event.value = (event.value == "")?"":"Invalid Value";

 

 

 

2 replies

try67
Community Expert
Community Expert
February 17, 2022

> I do not want this to be a document level calculation.

Why not? That doesn't make sense, unless you only want to do it for one field.

It's better to have a doc-level function and then call it from each field you want to apply it to.

Otherwise, you risk having to go back later on to all fields and edit them one by one if you want to change something in the code.

Participant
February 17, 2022

I probably don't understand what the Document level script would do...

 

My form has 10 lines for 10 "Loads". For each Load line the user enters, temperature, relative humidity, wind speed, and the time the batch was loaded into the helicopter. I want them to manually enter the time, but I want it to be as easy as possile. I don't want them to have to type the ":". I want to allow them to type 1515 and the form records it as 3:15 pm. They need to do it for each load. 

 

I did have the field setup to autopopulate the time "on focus", but that was too constricting for my application. The user may forget to enter this data for 20 or 30 minutes and I ant them to be able to enter the actual load time and not the time the field was clicked.

 

I really know nothing about scripting. I am a Google then copy-paste and alter someone elses script for my needs type of person.

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
February 17, 2022

To do this you need a custom format script.  A format script changes how the data is displayed, it does not change the actual value of the field.

 

if(/^\s*(\d{1,2})(\d{2})\s*$/.test(event.value))
{
    var nHour = Number(RegExp.$1);
    event.value = ((nHour>12)?nHour-12:nHour).toString() + ":" + RegExp.$2 + ((nHour >= 12)?" pm":" am");
}
else
   event.value = (event.value == "")?"":"Invalid Value";

 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
ls_rbls
Community Expert
Community Expert
February 17, 2022

If I understood correctly, you are asking if, when the users of the PDF enter hours and minutes expressed in Military Time (also referred to as 24 hour clock), the value will change or be converted to Ante Meridiem (AM) / Post Meridiem (PM) format (also referred to as 12 hour clock).

 

This may be possible with a custom keystroke script or a custom format script, but be advised, the calculating part of the script is not that trivial.