Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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;
}
}
})();
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
}
}
})();
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Excellent! It worked and I appreciate the expert guidance on this issue. Now, what do you recommend so that I can learn how to do this in the future?
Also, one question relating to this specific field and how it functions. Can I modify the alert that comes up when a user enters an invalid value? I would like to give the user some guidance so that they know what they are doing wrong. That way they aren't confused and they can fix the error without contacting me for assistance.
Great solution overall and thanks again for your help and any additional insight that you may be able to provide!
Dustin
Copy link to clipboard
Copied
Regarding the message, that's generated by the line of code that contains the app.alert statement. You can change it to say whatever you want.
There are a lot of great tutorials on a number of JavaScript and forms related topics at AcrobatUsers.com, mostly authored by Thom Parker.
You should also have the Acrobat JavaScript documentation, which now is included in the Acrobat SDK as a PDF. You can access it online at: http://www.adobe.com/devnet/acrobat/javascript.html
Another good resource for understanding how PDF forms work is Dr. Story's AcroTex blog, #12-25 in particular: http://www.math.uakron.edu/~dpstory/pdfblog.html
Copy link to clipboard
Copied
Thanks for all of the information! I am definitely learning how to do this stuff but I'm sure it takes years to fully understand how to write code like this. I will definitely get a few of these resources to keep here at my office for future reference. Thanks again for the input and expert advice!
Dustin
Copy link to clipboard
Copied
Is there a way to add multi-line support? Say a multi-line form field like:
999-99999A
999-99999B
999-99999C
Tried some variations of /n /t /r but nothing seems to work. What I have now is:
function Format(s) {
// blank
if (!s) return true;
// Set
var aRE = [
/^\d{3}-\d{5}[A-Z]{1}$/,
/^\d{3}-\d{5}}[A-Z]{1}\n\d{3}-\d{5}[A-Z]{1}$/,
/^\d{3}-\d{5}}[A-Z]{1}\n\d{3}-\d{5}}[A-Z]{1}\n\d{3}-\d{5}[A-Z]{1}$/,
];
// See
return AFExactMatch(aRE, s);
}
Copy link to clipboard
Copied
Bah. changed \n to \r and that was it. Not sure why it didn't work earlier but fine now.
Thanks for the helpful post!

