Skip to main content
Participant
February 4, 2025
Question

JS omit non-numeric characters, when converting time?

  • February 4, 2025
  • 1 reply
  • 731 views

Hello, I am a noob with zero JavaScript training, sorry if my descriptions are confusing.

 

I am trying to get Time Feilds to convert military time into standard time, so that it displays as H:MM tt. I googled and found a Validation script that seems to work well (attached).

 

However, I realized soon after, that unless I also type colon ":" I get an error message, and the fields won't recognize that I am trying to input a time. This also happened when accidently typed in a semicolon “;”. 

 

I have used forms in the past that automatically convert '0630' or '630' to "6:30 am" so I knew it was possible to leave out the colon, but as I continued researching it, I am realizing it might not be as simple as I thought. 

 

If possible, I want the fields to only consider numbers when converting to standard time, and also omit or not allow for any non-numeric characters to be typed in. Is this possible to accomplish?

 

Any and all advice is greatly appreciated, Thanx.

1 reply

Thom Parker
Community Expert
Community Expert
February 5, 2025

Good choice with the validation script. This is indeed the correct place to do the conversion. 

To do this correctly you'll need to write code that analyzes the entered value to determine how the script should proceed. Should it leave the value as is or do a comversion? The best way to do this is with a regular expression. 

https://www.pdfscripting.com/public/Pattern-Matching-with-Regular-Expressions.cfm

 

The script should test for a standard time input first. Then test for military time. If both tests fail, then it's not a valid input. 

 

Here are some tips on scripting in Acrobat:

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
ke_6938Author
Participant
February 5, 2025

Thank you for the Reply Thom, I'm still going over the resources you provided and I think I get the gist of what you're saying, though I might be digging myself deeper into a hole. 

 

Goal is to first call out the Time fields (ideally with a document level script?), check for Standard time then Military time, from there I should be able give additional instructions, for the feilds to behave a certain way.

 

I am fearing to do this I will also need to use a custom formatting script that I can build off of, rather than rely on the preset formatting for Time. 

 

Question: So additionally I am aware of a way to replace the value of a field using:

event.value = event.value.replace(/;/g, ":");

//replace ";" with ":"

 

I am thinking I could somehow use this method to simply replace unwanted characters with ":" since the field needs it anyway for stadard time to execute, am I at least on the right track?

Thom Parker
Community Expert
Community Expert
February 5, 2025

That's one approach.  However, you can't catch and fix all the possible errors that might be made entering a time. A more universal approach is to test for valid inputs and reject invalid inputs with a warning to the user. 

 

For example, here's a regular expression for detecting a valid regular time input.

 

var rgStdTime = /^\s*([01]?\d)\:([012345]\d)\s*((?:am?)|(?:pm?))\s*$/i;

  

Here's one that will match military time.

var rgMilTime = /^\s*([01]?\d)\:?([012345]\d)\s*$/i;

 

In both expressions the numbers are marked as subexpressions, so they can be extracted for reformatting. 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often