PDF-Forms is a service provided by PDFzone.com | http://www.pdfzone.com/ __________________________________________________________________

This question has been asked here and in the other "usual suspect" mailing list/forum. Because it is indeed an interesting question, and the answers so fare showed that the question was not really read or understood, I repeat the answer in both places. If anyone might feel annoyed by the double-posting, I apologize.

The question was to establish a field which allows the entry of numeric values, but prevents the user from entering more than two digits after the decimal point.

Simply setting the field format to "Number" and setting two digits after the decimal point is not sufficient, because in this case, the displayed value is indeed limited to 2 digits after the decimal point, but the value remains what the user entered. And as we have seen in some earlier discussions about rounding etc. this may cause discrepancies between the result based on what is displayed and what is calculated.

Therefore, we have to look for something else.

And now, meet a new friend: Regular Expressions.

Regular Expressions and their according object in JavaScript (the Regular Expression Object) are extremely powerful for pattern matching and doing something with its results. The Regular Expression Object is part of the Core JavaScript (and therefore not described in the Acrobat JavaScript documentation; you'd have to go to the JavaScript documentation page on the Netscape Developer site to get the JavaScript 1.5 Core Specification and Guide).

What we have to match is a value which may or may not have a minus sign, then a non-defined number of numeric characters, a decimal point, and then up to 2 more numeric characters. If our field entry matches this, things are OK, otherwise, we have to refuse the data entry.

For implementing, we have first to know which event we are going to use. For that, the Keystroke event is obvious, because we want to catch erroneous entries when they occur ... at the keystroke. And using the Keystroke event is good anyway, because we have will be able to make extensive use of the Event object.

The next point is to determine what we are looking at when doing the data entry. This is the combination of what we already have, and what we add in the current keystroke event. This is the combination of event.value (that's what we already have) and event.change (that's what we are entering).

And last but not least, we have to assemble the pattern against which we will match our value. We want to analyze the whole combined string, which mean we start the pattern string with "^" (for Begginning of the word) and end it with "$" (for end of the word). Then we want to allow for none or exactly one "-" character, leading to "-?". Then we want to have a non-definied number of numeric characters. For numeric characters, we have the shortcut "\d", and adding the "*" for a non-defined number. Next is none or exactly one decimal sign, which we assume it were the period. This leads in analogy to the minus sign to "\.?" (the backslash is needed because the period has a special meaning in Regular Expressions). And finally, we allow for 0 to 2 numeric characters again, which leads to "\d{0,2}". The whole Regular expression assembles to the following literal:

/^-?\d*\.?\d{0,2}$/

For assembling the code, we determine the conditions. There are actually three conditions which have to be fulfilled to reject the entry.

1. We are not committing the value (we already tested everything).

2. We add something to the string (deleting a character shall always be allowed).

3. The pattern must not match (that is when the match() method returns null)

We also decide to give the user a feedback when something illegal is entered. We do that with the beep.


And with that, we have everything to put together our code for the Keystroke event of our field.


if (!event.willCommit
&& event.change.length > 0
&& (event.value + event.change).match(/^-?\d*\.?\d{0,2}$/) == null) {
app.beep() ;
event.rc = false ;
} else {
event.rc = true ;
}


And that should do it.


Note that the first three lines here (beginning with "if" and "&&", and ending with the "{") are actually one single line, and that they wrapped here for better display.


We could (I think it would be better anyway) make this to a function at document level, and then simply call this function in the Keystroke event.


Hope, this can help.



Max Wyss PRODOK Engineering Low Paper workflows, Smart documents, PDF forms CH-8906 Bonstetten, Switzerland

Phone:  +41 1 700 29 21
Fax:  +41 1 700 20 37
  or  +1 815 425 6566
e-mail:  mailto:[EMAIL PROTECTED]
http://www.prodok.com



[ Building Bridges for Information ]


______________________




Shameless Plug:

My next conference appearances and workshops:
• Conference presentations at the 2004 Symposium of the BFMA, May 23 to 27 in Reno, Nevada (http://www.bfma.org) and pre-/post-conference workshop, May 22/23 and 27, organized by essociates Group (http://www.essociatesgroup.com/AdvancedAcrobatForms.htm)
• And, as always, available for on-site workshops/tutorials/consulting.



_________________________





SITUATION
I'm using Acrobat v6, and though I've set the field format to be a Number
with "2" decimal places, the fields still allow the user to input more than
two digits after the decimal point. Then, when the user tabs out of the
field, the extra digits are removed.


QUESTION
Does anyone know a way to establish a Number field with 2 decimal places
that does NOT allow the user to input more than two digits after the decimal
point?




To change your subscription:
http://www.pdfzone.com/discussions/lists-pdfforms.html



Reply via email to