Skip to main content
Participant
April 6, 2011
Answered

Custom Format Script Help

  • April 6, 2011
  • 2 replies
  • 26541 views

I am not too familiar with scripting but am hoping someone can provide some guidance or at least somewhere online that will show me how to do this.  Here's what I'm trying to do:

I have a text field that I need users to enter a specific number format in.  They have three choices of formats but only one type must be entered.  If I go by the arbitrary format options, they would look like this: 99-9999999OO, 999-99-9999, or 999999-99999.  Now, is there a way to write a script so that it forces the user to enter the text/number in one of those three formats only?  I am able to set up an arbitrary mask and get one of them but I need to provide options to the user.

If there is some good reading online where I can figure this out myself, let me know.  Otherwise, any input would be greatly appreciated.

Thanks,

Dustin

    This topic has been closed for replies.
    Correct answer George_Johnson

    As mentioned, it's easiest to use regular expressions. Here's a function that you can place as a document-level function and call it in the Keystroke event:

    function checkFormat(s) {

        // A blank field is OK
        if (!s) return true;

        // Set up array of regular expressions
        var aRE = [
            /^\d{2}-\d{7}[a-zA-Z0-9]{2}$/,  // 99-9999999OO
            /^\d{3}-\d{2}-\d{4}$/,          // 99-99-9999
            /^\d{6}-\d{5}$/                 // 999999-99999
        ];

        // See if string matches any of the regular expressions in the array
        return AFExactMatch(aRE, s);

    }

    Here's what you'd do in the text field's custom Format script:

    // Custom Keystroke script for text field
    (function () {

        // When the user has committed the value...
        if (event.willCommit) {

            // ...check to see if what was entered is valid...
            var isValid = checkFormat(event.value);

            // ...and if not, alter the user and reject the value
            if (!isValid) {
                app.alert("Please enter a valid value.", 1);
                event.rc = false;
            }
        }

    })();

    2 replies

    George_JohnsonCorrect answer
    Inspiring
    April 6, 2011

    As mentioned, it's easiest to use regular expressions. Here's a function that you can place as a document-level function and call it in the Keystroke event:

    function checkFormat(s) {

        // A blank field is OK
        if (!s) return true;

        // Set up array of regular expressions
        var aRE = [
            /^\d{2}-\d{7}[a-zA-Z0-9]{2}$/,  // 99-9999999OO
            /^\d{3}-\d{2}-\d{4}$/,          // 99-99-9999
            /^\d{6}-\d{5}$/                 // 999999-99999
        ];

        // See if string matches any of the regular expressions in the array
        return AFExactMatch(aRE, s);

    }

    Here's what you'd do in the text field's custom Format script:

    // Custom Keystroke script for text field
    (function () {

        // When the user has committed the value...
        if (event.willCommit) {

            // ...check to see if what was entered is valid...
            var isValid = checkFormat(event.value);

            // ...and if not, alter the user and reject the value
            if (!isValid) {
                app.alert("Please enter a valid value.", 1);
                event.rc = false;
            }
        }

    })();

    Participant
    April 7, 2011

    I am working in Adobe Acrobat Pro 9 and from looking online I don't see where to input document-level functions.  Is there another program I should be using besides Pro?  I am somewhat limited with the software right now so hopefully I can do it with either Pro or LiveCycle Designer because that's all I've got.  These scripts look great though!  So how can I add a document-level function to this document or can I with what I have?

    Inspiring
    April 7, 2011

    Look under "Advance => Document Processing => Document JavaScripts". You will open a new pop-up window, enter name of script to add and click "Add". The JavaScript editor will open and you enter your code.

    Inspiring
    April 6, 2011

    You can do this with the RegExp object, but I have not seen many online resources about using this object in Acrobat JS forms. John Deubert of Acumen Training wrote a book titled Extending Acrobat Forms with JavaScript that cover the use of the RegExp for formatting, keystroke validation, and formatting.