formatting time field HMM to H:MM

New Here ,
Nov 02, 2019 Nov 02, 2019

Copy link to clipboard

Copied

How to format text field for time so input could be  H.MM or H,MM or HMM and output was always H:MM? User to be able to enter the time in any of these formats, but always appears as H:MM in the text field.

TOPICS
Acrobat SDK and JavaScript, How to, PDF forms, Windows

Views

84

Likes

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
Adobe Community Professional ,
Nov 02, 2019 Nov 02, 2019

Copy link to clipboard

Copied

Do you have the ability to edit this form?

 

The time format was set by the form creator. If you want to change it. You have to change the field formatting. 

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

Likes

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 ,
Nov 02, 2019 Nov 02, 2019

Copy link to clipboard

Copied

I am the creator of the form. I want the user to enter, for example, 8.30 or 8,30 or 830 in the time field, and have javascript edit it at 8:30.

Likes

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
Adobe Community Professional ,
Nov 03, 2019 Nov 03, 2019

Copy link to clipboard

Copied

So you want the field value to be entered in a variety of formats, and then converted to a standard fomat after entry? 

 

This is possible, but you'll need to write a set of custom scripts. 

1. Restricting entry to a specific set of format. This has to be done as a non-commit custom keystroke script.

2. Converting entered data into a standard format. This could be done as a will-commit custom keystroke or a validation script.

 

The 2nd part is easier. The way to do this coversion is to use a regular expression to recognize the front and end parts of the entered data and then insert the  ":".

Heres a script that will perform the conversion.

if(event.willCommit)

{

    event.value = event.value.toString().replace(/(\d{1,2})[\,\.]?(\d{2})/,"$1:$2");

}

 

The first part gets more complicated. Do you know anyting about scripting?

 

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

Likes

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 ,
Nov 04, 2019 Nov 04, 2019

Copy link to clipboard

Copied

I'm a beginner in scripting. I only partially understand this script. But it works perfectly! I see that it is necessary to limit the input to real time possibilities (0-24 and 0-59). Can you advise on this too?

Likes

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
Adobe Community Professional ,
Nov 04, 2019 Nov 04, 2019

Copy link to clipboard

Copied

LATEST

The keystroke script can accept or reject user input by returning true/false to event.rc.

This is easy for general, single key, filtering. but if you want to match a specific input it gets more complex.

Here's a script that puts the complete input together and decides if it is acceptable.

 

if(event.willCommit)
{
... conversion script...
}
else
{ // keystroke script
if(event.change.length > 0) { // Test only non-empty inputs, Empty input is accepted var nChars = event.selEnd - event.selStart; var aFull = event.value.split(""); aFull.splice(event.selStart, nChars, event.change); var strFull = aFull.join("");
event.rc = /(\d{1,2})[\,\.]?(\d{2})/.test(strFull);
}
}

 

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

Likes

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