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

Getting a field to accept numbers only and leading zeros.

StevenD
Registered: Oct 6 2006
Posts: 368
Answered

I'm trying to figure out how to get a field to accept numbers only. And if there is a leading zero it should keep it.

00125
12345
22356
04569

The easiest way to format a field for numbers is with the format tab in the field properites but it won't keep a leading zero as far as I can see. And I can't get regular expressions to work. Or at least I can't figure out how to make it work with a regular expression.

Will anyone help me figure this out? Please.

StevenD

My Product Information:
Acrobat Pro Extended 9.1, Windows
Dimitri
Expert
Registered: Nov 1 2005
Posts: 1389
Hi StevenD,

You can do this with a custom keystroke script using a regular expression. This is a copy-n-paste example from www.pdfscripting.com -

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

Hope this helps,

Dimitri
WindJack Solutions
www.windjack.com
www.pdfscripting.com
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You could also just add leading zeros. [url=http://www.acrobatusers.com/forums/aucbb/viewtopic.php?id=18041]Adding "Zeros" in front of a number ...[/url]

George Kaiser

StevenD
Registered: Oct 6 2006
Posts: 368
Thanks Dimitri. It does in deed help. This is great. A few questions if I may.

I forgot to copy and past the if statement part of the code and it seems to work with or without.

Tell me if I am reading this regular expression right. Start (^) and end ($) with any number of digits (\d*).

Still trying to understand what the rc event does.

StevenD

StevenD
Registered: Oct 6 2006
Posts: 368
gkaiseril wrote:
You could also just add leading zeros. [url=http://www.acrobatusers.com/forums/aucbb/viewtopic.php?id=18041]Adding "Zeros" in front of a number ...[/url]
Each item number is different so users needs to be able to an item number that may or may not begin with a zero.

65823

or

06859

Thanks for the suggestion though. I can see where that could come in handy for adding other kinds of prefixes to numbers.

StevenD

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
There are two different kinds of keystroke events. The Proper keystroke, which is called each time the user presses a key, and the WillCommit Keystroke, which is called when the user commits the field data by pressing the Return key or tabs/clicks out if the field. Each one of these events uses different event properties for different reasons. The "event.willCommit" property is used to differenciate which one is being called.

"event.rc" is used to accept or reject the particular user action. For a proper keystroke event, "event.rc=false" will reject keystrokes. For the WillCommit keystroke, "event.rc=false" will reject the entire input.

Your reading of the regular expression is correctly.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

StevenD
Registered: Oct 6 2006
Posts: 368
I'm coming back to this with another problem. I have a field that needs to put it's value into some other read only fields. I set up a calculation script in field "Txt.DeptID2" to get the value from the field "Txt.DeptID". Field "Txt.DeptID2" and several other instances of it are read only. The value that is in "Txt.DeptID" has a value of 0000000001 but it come out in "Txt.DeptID2" as 1. I need to keep the leading zeros.

Can I turn the value into a string in this case? I don't know how to do that or if it is even the best way.

StevenD

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
If the value in the first field has a lot of leading zeros then it is a text field and the value is a string. The second field also need to be a text field. Just having both fields as text should solve the reformatting issue.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

StevenD
Registered: Oct 6 2006
Posts: 368
Both fields have no formatting or in other words the formatting is set to none. There must be something in the calculation script that not working very well.

This is what I am using for a Custom Calculation script in the read only field.

var myVal = getField("Txt.DeptID").value;
this.event.value = myVal;

StevenD

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Sorry, I assumed this was a LiveCycle Form:( Try this code

event.value = getField("Txt.DeptID").valueAsString;

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

StevenD
Registered: Oct 6 2006
Posts: 368
I wish it was a LiveCycle form.

Thanks. This works great. In the mean time while I was waiting for a reply and trying things out I came up with this script that seems to work in a Custom Calculation script for the "Txt.DeptID" field. Your solution takes up a lot less space. So I think I will use it instead.

//var myVal = this.event.value;
//var myNewVal = myVal.toString();
//getField("Txt.DeptID2").value = myNewVal;

Where could I have found valueAsString? Is this a core JavaScript thing? Some times I'm not sure what to look for.

Anyway thanks heaps.

StevenD

Dimitri
Expert
Registered: Nov 1 2005
Posts: 1389
Hi StevenD,

valueAsString is in the Acrobat JavaScript Reference under "Text Field-Properties."

Hope this helps,

Dimitri
www.pdfscripting.com
www.windjack.com
StevenD
Registered: Oct 6 2006
Posts: 368
Thanks loads.

StevenD