Forms – The Importance of Initial Conditions

In programming, things that are left in an unknown state are dangerous. That’s why it’s usually a good idea to initialize variables with sane values as soon as they are created (and sanity-check incoming argument values before using them). But beginning programmers can’t be expected to know this stuff. Yet, it seems like an awful lot of people who are using JavaScript in PDF forms are (how shall we say?) inexperienced programmers.

I encountered a good example of this last week. A form designer, desperate to the point of tears (almost), wrote to me to beg for help with a form problem. The form would load okay, and would accept the user’s (textual) input into the first text field, but as soon as the field lost focus, an error alert would pop, saying something about an incorrect value. No format scripts were being applied, however, so it was a bit mysterious (at first) as to why the text field wouldn’t accept ordinary text without complaining.

It soon developed that the alert went away if I turned off ‘Auto-Calculate Field Values’ (in Tools:Forms). This meant that a distant numeric field (with a script that was being triggered by the auto-calc) was the source of the problem. I noticed that all the numeric fields had default values of 0.00 (which was not entirely rational, given some of the field names).

I didn’t have to dig very much further. The calculate script for the final numeric field on the page had a division of one field value by another field value. All you had to do to trigger the script was enter anything into any text field and tab to the next field. Since all values on the page started out at zero, this set up an automatic zero-divided-by-zero situation. Error city.

Prevention is better than cure

So let that be a lesson. First, whenever dividing by a variable, either add a tiny value to the denominator to keep it from being zero, or (better) put a conditional around the division to check for the denominator being zero. Secondly (and more important, arguably), intialize your form fields to rational values. Don’t just leave them blank or zero. If you do, you don’t know what kind of nonsense might happen.

You May Also Like

About the Author: Kas Thomas

Leave a Reply