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

Restricting user entered numeric values

pforms
Registered: Nov 17 2009
Posts: 87
Answered

Need a script for limiting a user to be able to enter only 0 or 3 in a numeric field

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Do you mean just the single digit 0 or 3, or only strings of 0's and 3's?

You can use the change event to restrict whats entered in the field. The new data entered into the field is in the "xfa.event.change" property. If you set this property to an empty string then there is no data entry. The "xfa.event.preText" property contains the data in the field before the user typed anything and "xfa.event.newText" property contains the data that will be in the field afterward. Using these values you can decide how to set "xfa.event.change".

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

pforms
Registered: Nov 17 2009
Posts: 87
User may enter only a single digit. 0 or 3
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Something like this would work
if(!/^[03]?$/.test(xfa.event.newText))xfa.event.change = "";

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

pforms
Registered: Nov 17 2009
Posts: 87
Nice. I hadn't thought of this. I was working towards this on "exit":

if ( (this.rawValue > 0) || (this.rawValue < 3) )
{
this.rawValue = null;
xfa.host.messageBox("Please enter a value 0 or 3.", "Invalid Value", 0, 0);
xfa.host.setFocus(this.somExpression);
}

I like your solution much better.