These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Limit Form Field to Alpha Characters Only

ccs717
Registered: Aug 18 2006
Posts: 20
Answered

Good Day,

I have a form field that needs to be filled out with letters only - no numbers or special characters. I created a custom keystroke script to provide exactly that formatting restriction, however it does not allow the use of the backspace or delete keys. This makes it very user UN-friendly.

Is this behavior to be expected with such a custom script? Or can someone suggest a script to allow letters and backspace/delete?

Are there any backward compatibility concerns if this PDF were to be used with, say Acrobat 5?

Many thanks.

Craig S.

My Product Information:
Acrobat Standard 8.1, Macintosh
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
It is certainly possible to create a keystroke script that behaves as you want, and it can be made backwards compatible to 3.02. Can you post what you have so far?

George
ccs717
Registered: Aug 18 2006
Posts: 20
Hi George,

Thanks for the quick reply. My code is below. I admit to knowing just enough to be dangerous when it comes to JavaScript, so if the code seems not well written, please let me know.

if (event.willCommit == false) {
event.rc = /^[a-zA-Z]+$/.test(event.change);
}

Craig
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Craig,

Reasonable start, but I would expand on that a bit. Begin by creating the following document-level JavaScript:

function chars_only_ks() {

// Get everything that the user has entered so far
var value = AFMergeChange(event);

// Do nothing if there are no characters
if(!value) return;

//If user attempts to enter an invalid sequence of characters, reject it
if(!AFExactMatch(/[A-Za-z]*/, value)) event.rc = false;
}

Note that the AFMergeChange and AFExactMatch functions are built-in convenience functions supplied by Acrobat and used by it for the various built-in Format/Keystroke options available.

Now that you've set up the above function, you can use the following as the custom Keystroke script for a field:

if (!event.willCommit) chars_only_ks();

And that should do it. If you're interested in studying the details of the two convenience functions so you understand exactly how they work, post again.

George
ccs717
Registered: Aug 18 2006
Posts: 20
Thanks again George. This is great.

As I said earlier, I know just enough to be dangerous so I can't honestly say I understand all of what your script is doing. When I have a moment I intend to study it more carefully.

Craig
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Note that the last line of the function can also be:

event.rc = AFExactMatch(/[A-Za-z]*/, value));


but sometimes it's good to be explicit when trying to convey the idea.

George