Acrobat 6: Field validation

Posted on 2004-11-26
Last Modified: 2008-02-01
I need to validate a field onBlur with JScript:

- should be numeric (1 to 9999999999)
- spaces should be trimmed

Question by:go4java
    LVL 44

    Expert Comment

    by:Karl Heinz Kremer
    Do you want the user to enter spaces? The most straight forward approach would be to only allow numeric information to be entered. You can do this by opening the properties dialog for your field. Then select the "Format" tab and select the format category "Number". Specify how many decimal places, then select the "Validation" tab and specify the limites (1 to 9999999999).

    If this is not sufficient, I can give you more information about how to do this with JavaScript.

    Author Comment

    Is more a regular expression: The first char should be 1 to 9 (not 0!) and the follwoing 9 chars should be 0 to 9.
    LVL 44

    Accepted Solution

    This took a little longer than I exptected: I run into a bug in Acrobat and had to find a workaround first. It looks like whenever I'm using AFMergeChange() in a custom keystroke script, Acrobat "forgets" the script.

    You need to use a custom keystroke script to do what you want to do. Such a script gets run for every keystroke that a field receives. Depending on the return value of the event, the keystroke is then either accepted, or rejected. In your case, you would reject all non-digit characters, and the zero for the first position, and also all characters after the string length exceeds the  number of valid digits.

    Open your form and select the Advanced>JavaScript>Document JavaScripts... menu item. Create a new script that you name "validateField" and click on the "Add" button. In the edit field, add the following script:

    function validateField(ev)
        var newValue = AFMergeChange(ev);
        var re = /^[1-9][0-9]*$/g;
        var res = false;

        if (newValue.match(re))
            if (newValue.length <= 10)
                res = true;

        return res;
    // end of script

    Then open the properties dialog for your form field and select the "Format" tab. Click on the "Edit" button for the "Custom Keystroke Script". Then use the following command as your custom keystroke script:

    event.rc = validateField(event);
    // end of script

    This will assign either true or false to the rc property of the event object. This either accepts or rejects the keystroke.

    The actual validation routine uses a regular expression to first make sure that you have a number that starts with 1 to 9, and then iin a second step makes sure that the string is 10 characters or shorter.


    Author Comment

    Thank you,
    I found the workaround:

    1. onBlur

    - conversion if field content to String! (--> 01 will not be parsed to 1)
    var sNodeString = sNode.valueAsString;

    2. Test field content with regular expression

    var regExpr =  /^[1-9][0-9]{0,9}$/;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Acrobat’s JavaScript is a great tool to extend the application, or to automate recurring tasks. There are several ways a JavaScript can be added to the application or a document (e.g. folder level scripts, validation scripts, event handling scripts,…
    Power PDF ( is the newest product from the Document Imaging division of Nuance Communications ( It is available in two editions — …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now