Expand my Community achievements bar.

How do I restrict a numeric field to an exact amount of digits, using Script Editor?

Avatar

Former Community Member

I am creating a form where I have a couple of fields that require an exact number of digits, no more and no less, for accurate reporting purposes. We have to report certain data to the government and this data is audited by the government on a regular basis--if the reported data does not match documentation on file, we have a "hit." In an effort to reduce "hits" caused by improper completion of the form, I'm looking to set these two fields so that an exact amount of digits must be input.

The two fields fields I'm concerned about are the County Code and the Tract Code. These codes are generated when we input an address into a government website, and what we report has to exactly match what the website generated. The County and Tract Codes will now have 5 and 11 digits, respectively--however, this is a drastic change from the 3 and 5 digits that these codes have been for years and years. I would like to eliminate the possibility of staff inputting the "old" code formats.

I have been able to use the Display, Edit, and Validation patterns in order to set a "maximum" amount of digits, i.e. for the County Code, no more than 5 digits can be entered: num{zzzz9}. This eliminates half my problem. But the other half of my problem is that it allows for less than 5 digits to be input.

I've been able to figure out that my solution lies in the Script Editor feature. However, I'm not computer savvy enough to figure out the scripting, in order to accomplish setting a "minimum" number of digits. What Event should have the script (Enter, Exit, Validate are my best guesses)? Should the language be FormCalc or JavaScript? And most importantly, how exactly should the script read?

I've been Googling for hours trying to figure this out, but have had zero luck--HELP!!

1 Reply

Avatar

Level 10

You can add a small script in the fields exit event to check the length and warn the user.

if (this.rawValue.toString().length !== 5) {

xfa.host.messageBox("Enter exactly 5 digits!");

xfa.host.setFocus(this.somExpression);

}