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!
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.
You have the option to either select an existing PDF form document or scan a document to start preparing the form.
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.
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.
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”);
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.
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.
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,”);
Follow the steps below to enable the console:
Open Adobe reader
Enable “Show console on errors and messages”