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

Limiting field to accept only characters & leading zero issue.

Philus03
Registered: Sep 24 2009
Posts: 4
Answered

Hi, I have two questions. Apologies if these have already been answered.

Question 1: Is there a keystroke command to only allow characters in a test field? i.e. A-Z, a-z only - no numbers?

Question 2: I also need to allow only numbers in another field - AND keep the leading zero.
Dimitri posted an answer to this previously

if(!event.willCommit)
event.rc = /^\d*$/.test(event.change);

However, it will only work when the format tab is set to 'None' - which also allow characters to be inputed.

Is there a way to keep leading zeros AND limit the field to numbers only?

Thanks for any help / suggestions.

Phil

My Product Information:
Acrobat Pro 7.0.9, Macintosh
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
1. Yes you can use the RegExp 'test' method for the custom keystroke JavaScirpt.

// only if event not committedif (event.willCommit == false) {// define RegExp for alpha only any casevar RE_Alpha = /^[A-Za-z]$/;// use RegExp test to set event return codeevent.rc = RE_Alpha.test(event.change);}

2. You need to the field to be a text field without formatting to allow for the display of leading zeros and that may mean you need to force the string type for the value.

// custom keystroke scriptif (event.willCommit == false) {event.rc = /^\d+$/.test(event.change);}

// custom format scriptevent.value = ' ' + event.value;

You will need to use the 'Number()' constrictor for any fields accessing this field for numeric operations.

George Kaiser

Philus03
Registered: Sep 24 2009
Posts: 4
Thanks gkaiseril for both solutions.

They work a treat!

Cheers,
Phil
Philus03
Registered: Sep 24 2009
Posts: 4
Hi gkaiseril,

I've just realised that within this characer limited field, I will also need to allow:

1. 'spaces' to be inserted (i.e David Smith instead of DavidSmith)

2. allow characters to be 'deleted'. i.e if the user makes a mistake inputting.

Is this possible?

Thanks again for your help. Its been invaluable.

Phil
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
The code uses the Regular Expression object and strings. These strings are quite flexible but you need to know the meaning of the characters. Thom Parker wrote [url=http://www.acrobatusers.com/tutorials/text-matching-regular-expressions]Text matching with regular expressions[/url] which starts to discuss these values and one of the methods of the RegExp. He also provides links to additional resources for understanding the RegExp object.

// upper case and lower case letters and spaces only// only if event not committedif (event.willCommit == false) {// define RegExp for alpha and spaces only any casevar RE_Alpha = /^[A-Za-z ]*$/;// use RegExp test to set event return codeevent.rc = RE_Alpha.test(event.change);}

// numeric custom keystroke script with white spaceif (event.willCommit == false) {event.rc = /^[\d]*$/.test(event.change);}

RegExp methods can be used for keystroke checking, formatting, and validating input, but the strings can be quite complex. Acrobat uses RegExp for the various standard field formats provided by the drop down options on the format tab.

George Kaiser

rbr00x
Registered: Oct 14 2009
Posts: 26
I am using this code in Acrobat 9 to test for numeric entries, but I also need to allow for a decimal point and this doesn't seem to accomdate that. What do I need to change?

if(!event.willCommit)
event.rc = /^\d*$/.test(event.change);

Thank you,
Renee