These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

How to set the number format for a text field programmatically

dotnetwiz
Registered: Jun 8 2010
Posts: 57
Answered

I'm trying to figure out how to programmatically set a text field's Format property to Number, to allow only numbers to be entered into the field.

In my case, I want the user to enter only digits into a comb field of 8 characters.

I can do this interactively, but I need to set this property programmatically with Javascript.

I've tried something like:

this.getField("fieldname").setAction("Format", 'AFNumber_Format(0, 1, 0, 0, "", false)');

but doing so sets the field to Custom format, and still allows non-numeric input.

Setting this property interactively results in Acrobat not-allowing non-numeric input at all, which is the desired behaviour I'm looking for.

Help will be much appreciated.

My Product Information:
Acrobat Pro Extended 9.3.1, Windows
dotnetwiz
Registered: Jun 8 2010
Posts: 57
I'm also wondering if there is any way to query a field's actions/events. Something akin to a getAction method, that tells what is currently registered to run for a particular event.

This would potentially solve my problem, for I could query a field's Format property, after setting it up interactively first.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
Maybe will also need to set the custom keystroke script. The Keystroke is the item that determines what keystrokes will be accepted or rejected.

You are using a very rarely documented function of Acrobat and this technique will no longer work with Reader and might not work with 3rd party viewers.

Once a field's various actions are set there is not JavaScirpt method for inspecting this data.

George Kaiser

dotnetwiz
Registered: Jun 8 2010
Posts: 57
Many thanks!

I would have bet that there was a scriptable way to set the option available through the UI
Your response will save me a lot time looking for something that's not available!

Cheers!
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
When Adobe had the aform.js file uncoded, one could see the actual code of the Acrobat provided functions. There was an 'AFNumber_Keystroke()' function with the same parameters as the 'AFNumber_Format()' function.

If you use 'console.println(AFNumber_Keystroke)' code in the JavaScript console, you will get a code listing of the function. In fact some of the Adobe provided code from version 3 to 4 was changed.

Again Adobe has not documented these functions and does not guarantee their inclusion or not being modified at a later date.

George Kaiser

dotnetwiz
Registered: Jun 8 2010
Posts: 57
That's very helpful. You're opening up new vistas for me. :)

Thanks a bunch!
dotnetwiz
Registered: Jun 8 2010
Posts: 57
Setting the custom keystroke script to the 'AFNumber_Keystroke()' function does indeed set the field's Format property to Number in the user interface, but the parameters for the decimal place and separator specified in the function do not stick--Acrobat resorts to the default values for those.

I suppose I could just create my own streamlined custom keystoke script based on the code listing for the 'AFNumber_Keystroke()' function, that I can now inspect, thanks to your magic trick!

My script would only allow numeric input (no decimals), and beep if anything else is typed.

Cheers!
dotnetwiz
Registered: Jun 8 2010
Posts: 57
Here's what I came up with, and it works perfectly:

var l_oDigitsOnly = /\d*/i;
if (!(l_oDigitsOnly.test(event.change) && RegExp.lastMatch == event.change))
{
app.beep(0);
event.rc = false;
}