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

JS omit non-numeric characters, when converting time?

Community Beginner ,
Feb 04, 2025 Feb 04, 2025

Copy link to clipboard

Copied

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.

TOPICS
Create PDFs , JavaScript , Modern Acrobat , PDF , PDF forms

Views

189
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 ,
Feb 04, 2025 Feb 04, 2025

Copy link to clipboard

Copied

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

Votes

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 Beginner ,
Feb 05, 2025 Feb 05, 2025

Copy link to clipboard

Copied

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?

Votes

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 ,
Feb 05, 2025 Feb 05, 2025

Copy link to clipboard

Copied

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

Votes

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 Beginner ,
Feb 26, 2025 Feb 26, 2025

Copy link to clipboard

Copied

Thank you so much again Thom I really do appriciate it.

I am so sorry for not responding ,I ended up getting pulled away from this to work on other projects.

 

I might be off and on, but i want to try get back to this before i completly lose my train of thought.  I went over the epression you provided, (really tried to study it ) Thank you again.

 

I tried a few things but I don’t think I have the right function/method correct, I kind of lost my train of thought after being away from the form. below is the last thing i tried, 

 

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

 

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

 

if (event.value = rgStdTimeNOCOLON ) {event.value.replace(rgStdTime);}

 

question, do I also need a 'Custom Format' for h:mm:tt ? I am wondering if the preset formatting is messing with what im trying to accomplish.

Votes

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 ,
Feb 26, 2025 Feb 26, 2025

Copy link to clipboard

Copied

LATEST

The regulare expressions I provided are not for correcting input, but for detecting incorrect input. 

Here's an article on how to use regular expressions:

https://www.pdfscripting.com/public/Pattern-Matching-with-Regular-Expressions.cfm?sd=40

 

 

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

Votes

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