Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

When someone is typing in a letter into a number field, how can I get an error message to pop up?

New Here ,
Mar 18, 2016 Mar 18, 2016

I am trying to make an interactive form which is looking for the working hours for the week.  From time to time, people are absent minded as to why they can't type in a field and don't realize they should be typing in a number. When I set a field to Number, it won't accept letters, but it also does not warn the user that they should be inputting numbers.  Is there a validation needed for this?

I am very new to acrobat pro, but do have some experience with coding.  Any help on this is greatly appreciated.

TOPICS
Acrobat SDK and JavaScript , Windows
756
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 18, 2016 Mar 18, 2016

You would have to create your own custom Keystroke and Format JavaScripts. It would be easiest to copy most of the built-in one that Acrobat/Reader (and some other non-Adobe PDF viewers use), which you can find out more about by entering the following in the interactive JavaScript console, placing the cursor on the line of code, and pressing Ctrl+Enter:

AFNumber_Keystroke

It will spit out something looking like this:

function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency, bCurrencyPrepend) {

    var value = AFMergeChange(event);

    var commit, noCommit;

    if (!value) {

        return;

    }

    if (sepStyle > 1) {

        commit = AFNumberCommaSepCommitRegExp;

        noCommit = AFNumberCommaSepEntryRegExp;

    } else {

        commit = AFNumberDotSepCommitRegExp;

        noCommit = AFNumberDotSepEntryRegExp;

    }

    if (!AFExactMatch(event.willCommit ? commit : noCommit, value)) {

        if (event.willCommit && !event.silenceErrors) {

            var cAlert = EScriptString.IDS_INVALID_VALUE;

            if (event.target != null) {

                cAlert += " [ " + event.target.name + " ]";

            }

            app.alert(cAlert);

        } else {

            app.beep(0);

        }

        event.rc = false;

    }

    if (event.willCommit && sepStyle > 1) {

        strval = event.value;

        commas = new RegExp;

        commas.compile(",");

        strval = strval.replace(commas, ".");

        event.value = strval * 1;

    }

}

You can either modify this function to deal with keystrokes that it disallows and place it in a document-level JavaScript, or leave it as-is and add code that's called before you call this function to deal with the non-numeric keystrokes.

You will also need to call the same Format script that Acrobat/Reader uses. I realize all of this may be a bit confusing, so if you need more help or sample PDF that demonstrates this, post again.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 21, 2016 Mar 21, 2016

Thanks for the response.  As you said it is a little confusing, so perhaps a sample PDF would help.  I know how to add document level javaScript, but how would I make this function apply to a specific field, Would I call this function in the "Run a javascript" for the field?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 21, 2016 Mar 21, 2016
LATEST

I'll be able to post a sample later today.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines