Skip to main content
Participant
November 10, 2017
Answered

How to use a Document Script to make Multiple Text Fields accept just 4 numbers?

  • November 10, 2017
  • 1 reply
  • 838 views

Hello,

I am trying to use a Document Script to limit Multiple text fields (68 text fields) to accept just 4 numbers in an Interactive Pdf form.

A user is given 26 Numbers, all 4 digits, starting with a 0 sometimes.

To order a product, they need to enter one of the 26 Numbers in a text Field.

The text Field names are "Product Number 1", "Product Number 2".... with Spaces.

I came across this script to make sure the field just collects 4 Number:

event.value=event.value ? util.printf("%04d", event.value): "";

I don't know how to use it.

1. Would the Field Names with spaces be a issue?

2. I would like to be able to enter the script in just 1 place and not every text Field.

Thanks,

Falguni.

This topic has been closed for replies.
Correct answer try67

The doc-level function can be something like this:

function validate4Digits() {

     if (event.value) event.rc = /^\d{4}$/.test(event.value);

}

You would need to apply a call to this function from the validation script of each field, though.

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
November 10, 2017

The doc-level function can be something like this:

function validate4Digits() {

     if (event.value) event.rc = /^\d{4}$/.test(event.value);

}

You would need to apply a call to this function from the validation script of each field, though.

FalguniPAuthor
Participant
November 10, 2017

Thanks Try67!

It works!!! I add this code in every field > Properties:

if (event.value) event.rc = /^\d{4}$/.test(event.value); in the Validate Tab > Run Custom Validation Script

I couldn't use the Doc Level Function, it didn't work for me. Don't know what is missing...

In Acrobat X Pro > Tools > Forms > Edit > Other Tasks > Javascripts > Document Javascripts > Script Name "validate4Digits" > Add > Edit > In this window, I added the whole code:

function validate4Digits() {

     if (event.value) event.rc = /^\d{4}$/.test(event.value);

}

Then I came to the text field > Ctrl + I to Open text field Properties > Run custom validation script > Edit >  Pasted this code:

function validate4Digits();

But it didn't work. Don't know what I missed.

Thank you!

try67
Community Expert
Community Expert
November 10, 2017

The call to the function should just be:

validate4Digits();

You should really use a doc-level script for this. Imagine you wanted to add an error message if the wrong value is entered. With the doc-level script you would only need to edit it once. With the script in each field you would need to edit all of them...