Recently I have got some interesting questions and one of them that has been asked again and again is how to make fields that can validate field value in an AcroForm. Some of our users already know that there is an option that can be used to restrict the field value, but there is another way: Right PDF Editor/ Gaaiho Doc can let you do it with a simple script.
But first let’s take a look at how to set a value range without using any script. For example, suppose you want to create a text field with a value between 50 (min) and 100 (max). See below for instruction.
Note: if the value is lower than 50 or greater than 100, a popup message will show this value is invalid and detail the acceptable range.
1. Open a file in Right PDF Editor/ Gaaiho Doc and select "Form" > "Form" > "Text Field".
2. Click-drag the pointer to create a text field. In the Text Field Properties dialog box that opens, select the “Validate” tab and check “Validate field value” to make the following validation options available. If you set this field as a numeric field by checking “Number” under the “Format” tab, the “Field value is” option will be enabled, which allows you to set a value range with ease.
3. Define the value range by simply typing in numbers.
What about executing a custom validation? Let’s say you want to create a text field that allows characters and space only. See below for instruction.
Note: if the value includes numbers, a popup message will tell the user what the right type of value is for this field.
1. Open a file in Right PDF Editor/ Gaaiho Doc and select "Form" > "Form" > "Text Field".
2. Click-drag the pointer to create a text field, and the Text Field Properties dialog box pops up.
3. Select the “Validate” tab and check the “Validate field value” option to make the validation options available. Click “Run custom validation script” and click the “Edit…” button.
4. In the “JavaScript Editor” window, type in the script:
Or simply copy and paste the sample script shown below:
var number = /[0-9]/; if (number.test(event.value)) { app.alert("Please enter Chinese characters, alphabetic characters and space only."); event.rc = false; }
Similarly, there can be a “card number” field that allows numbers only. If something other than numbers is entered in the field, an error message will be shown.
1. Select "Form" > "Form" > "Text Field" and click-drag to create a text field. Don’t forget to set “Comb of 4 characters” on the “Options” tab for each field.
2. On the “Validate” tab, click “Run custom validation script” and the “Edit…” button.
3. In the new window, type in the script for the card number field:
The script includes not only a check for whether the value includes numbers only, but also for whether the value of each character is between 0 and 9. To make things easier, copy and paste the script shown below:
event.rc = true; var regex = /^[0-9][0-9][0-9][0-9]$/; if ( !regex.test(event.value) && event.value) { app.alert("Numbers only"); event.rc = false; }
In the script, you see “value” is used to communicate with the current value of the field and the “rc” is used to tell whether the validation was successful or not.
You might notice that the validation function takes place only when the focus leaves the field, meaning you have to actually press Enter or click outside the field. In case an error happens, the invalid value will be deleted and the user has to enter the data again, which could be quite a hassle given a lengthy value. It would be better if the field keeps the value so that the user can correct typos and press Enter to proceed to the next field.
Another way is to mark the field out in the color you specify so that the user would know the value of this field needs to be corrected and there is no need to retype the data again. See the script below:
var number = /[0-9]/; if (number.test(event.value)) { app.alert("Please enter Chinese characters, alphabetic characters and space only "); getField("Text7").strokeColor = color.red; } else { getField("Text7").strokeColor = color.black; }
You can also create a “Validate and print” button, which serves to check if all the fields specified are filled out, at the bottom of the form. It will also display the Print dialog box after validation. The button checks the entire form in the order of the form structure. If a field is empty, you will get an error message. This validation function is associated with Document JavaScript so you have to create a document JavaScript before adding a “Run a JavaScript” action to the button.
1. Create a button named “Validate and print” and configure its appearance.
2. Select "Advanced" > "JavaScript" > "Document JavaScript". In the JavaScript Functions dialog box, click the “Add…” button and give the document JavaScript a name. Click Next.
3. In the Create and Edit JavaScript dialog box, enter the script:
Or copy and paste the script shown below:
function isFieldEmpty(fieldname) { var field = getField(fieldname); if ( field.type == "combobox" ) { if (field.currentValueIndices == 0) { return true; } } else if ( field.type == "text" ) { if (field.value.length == 0) { return true; } } return false; }
The script varies with the type of field. The sample script above is for validating Text fields and ComboBox fields only.
4. Click OK and Close to finish.
5. In the Button Properties dialog box of the “Validate and print” button, select the “Actions” tab and select the action that triggers the script from the “Select Trigger” drop-down menu, and “Run a JavaScript” from the Select Action drop-down menu. Click Add….
6. In the JavaScript Editor dialog box, type in or copy and paste the script shown below and click OK. Note that you have to include all the fields you want to be validated in “if (isFieldEmpty(“…”))”. For each “app.alert (“…”)”, you can create an error message for each field when it is found empty.
if (isFieldEmpty("Text1")) { app.alert("ID is empty!"); } else if (isFieldEmpty("Text2")) { app.alert("Company name is empty!"); } else if (isFieldEmpty("ComboBox1")) { app.alert("Please select your preferences!"); } else if (isFieldEmpty("FirstField")) { app.alert(“Card number is empty!"); } else { print(); }
Lastly, I’d like to remind you that there are many ways to validate a form—but always make sure you are not making the form impossible to read.
Comments
0 comments
Please sign in to leave a comment.