Our Engineers can Write as well

Three Easy Steps to Validate Fields in Acrobat Form

We always look for the right data in forms and nothing annoys us like an empty field or incomplete entry. Validation of fields helps us easily avoid this scenario. Well, how are we going to achieve that? Using a custom validation script!

In an AcroForm, the user clicks the Submit button after filling in the necessary details. Here you can leverage JavaScript to validate all the fields: if any required fields are left empty or any field validation fails, etc.

Adobe has the built-in infrastructure to add JavaScript validation and allows you to control and fool-proof what your users enter in the AcroForm.

SCENARIO :

Here we shall talk about adding a text field that cannot include numbers and must be atleast 2 characters long. What if the user enters a text along with numbers, for example, Name01. We should see an error message that will inform the user about what type of data is valid for this particular field. Let’s see how we can do this in 3 simple steps.

STEP 1 :

Start the Acrobat Pro DC and select Tools >> Prepare Form.

acroform prepareform

You have the option to either select an existing PDF form document or scan a document to start preparing the form.

acroform selectfile
STEP 2 :

Create a text field and right-click to bring up the properties dialog for the field. Then select the “Validate” tab to see the available validation options.

acroform validatetab

By default, the field will not get validated. Numeric fields wouldn’t trouble us more, as there is a convenient way to validate them: Simply set up a value range (e.g., 50 to 100). However, text fields require little more effort and a custom validation script has to be run.

STEP 3 :

Click the “Edit” button. A new window will open up where you can edit the new script.

acroform addjavascript
CODE BELOW :

Check the Validation Code below:
var reExp = /^([^0-9]*)$/;
if (event.value == “” || (event.value.length < 2 || !(reExP.test(event.value))))
{
if(event.value == “”)
{
app.alert(“Name is Required”);
}
else if(event.value.length < 2 && !(reExp.test(event.value)))
{
app.alert(“Name must be at least 2 characters and non numeric”);
}
else if(event.value.length < 2)
{
app.alert(“Name must be at least 2 characters”);
}
else if(!(reExp.test(event.value)))
{
app.alert(“Please Enter non numeric values.”);
}
}

What if a user enters nothing in the text field and moves ahead to the next? The above script provides a solution for that too as it includes a check for an empty string.

acroform popup

When you play around with the function, you’ll notice that the validation function is called only when the focus leaves the field. Therefore you have to click outside the field for the error message to pop up.

LIMITATIONS :

Adobe Reader will not support some JavaScript methods such as String.trim(), String.match(“String”);

You will have to come up with a different method to trim the whitespace at the beginning or end of a string. However, this could be easily achieved with a regular expression:

var value = “ String ”;
value = value.replace(/^\s+|\s+$/gm,”);

It is cumbersome to check the available types of restraints every time and find out solutions for them. An easy option is to allow the Adobe Reader JavaScript console to throw errors.

Follow the steps below to enable the console:

  • Open Adobe reader

  • Click Edit >> Preference >> JavaScript

  • Enable “Show console on errors and messages”

acroform errormsg

In this way, you can identify the unsupported script methods without much effort. To learn more features, take a look at JavaScript Form Validation Script.

prabakaran.a

Contributed by:

Prabhakaran.A
Software Engineer

Your email address will not be published. Required fields are marked *