Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
I'll be able to post a sample later today.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now