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

Regular expression for a backspace?

nixopax
Registered: Feb 6 2009
Posts: 105
Answered

Hi there guys(and gals),

I'm working on a form to accept a telephone number that is a standard USA 7 digit number, with a separate field for the area code (Not the best method, I know) . So I set it to format as a phone number. Yet I do not want the hyphen (-) because the form is submitted to a database where the phone number is set to INT, so a hyphen will break the number. I also know that format isn't the value, but most people try to be helpful, and type the hyphen even though Acrobat interprets a phone number and automatically puts the hyphen for visual appeal. So I write a regular expression to accept only numbers in the !event.willCommit keystroke. All is good. But then I ran into the issue of not being able to backspace in the event the user makes an error.. So I figured I'd just look up the Javascript Regex value for a backspace. The exception is: [\b] So my code looked like this:

var backspacerxp = /\d[\b]/; // \d is for digits only
var test = event.change;
console.println(backspacerxp.test(test));
if(backspacerxp.test(test))
{
event.rc = true;
}

I could enter a number, and the console would show TRUE. But if I hit backspace, I'd get a FALSE. So I went some other routes. I've looked up the Binary, Octal and Hex values for the UTF code of backspace. I tried all those as well. Nothing worked. What am I doing wrong? Also, for the person that does know how to accept a backspace via Regex, does that also include the Delete key, as it is interpreted to delete the character in front of the cursor, rather than the one behind it? Or... if you can suggest a better method to go at this. The form is submitted to a PHP page, which I wouldn't have any issue scrubbing the text further, but I want to get it client-side, and use my server side technology after. The less work my server has to do, the faster the response back to the client and the lower the chance of timing out.

The documentation Acrobat provided for the AcroJS API doesn't really cover Regular Expressions in any detail (As it's not API, but rather a core function supported since ECMA 1.2), so I've resorted to other online resources. Perhaps someone here with extensive AcroJS history would know.

I look forward to any response.

My Product Information:
Acrobat Pro Extended 9.0, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
You cannot detect the backspace character, except by deduction perhaps, but there are better ways. For example, you can call the following function from the text field's Keystroke event:

// Function in a document-level JavaScriptfunction digits_only_ks() { // Get everything that the user has entered so farvar value = AFMergeChange(event); // If user attempts to enter an invalid sequence of characters, reject itevent.rc = AFExactMatch(/\d*/, value); }

like this:

// Custom Keystroke script for a text fieldif (!event.willCommit) digits_only_ks();

The AFMergeChange and AFExactMatch functions used above are built-in functions available for you to use. If you're interested in learning how they work you'll have to study their source code.

George
nixopax
Registered: Feb 6 2009
Posts: 105
Thanks George, that works great. You mentioned that I should study their source code, but I cannot find where it's being stored. I've searched for those specific functions, but find no mention other than its use in a few examples in the AcroJS reference, but nothing direct. Where is this javascript found in the Acrobat 9 environment?
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Open up the JavaScript console and type:

AFMergeChange

and press Ctrl + Enter and you will get a code listing (but no comments). Do the same for AFExactMatch.

George
nixopax
Registered: Feb 6 2009
Posts: 105
Excellent! I had no clue the console had such capabilities! This is amazing.

By the way, I'm submitting a bug report, as I've run into something rather weird when I use your code. I've recorded the bug using Captivate:[url=http://www.fileden.com/files/2006/7/21/139406/AFMergeChange%20bug_skin.swf]Video Player of Bug[/url] [url=http://www.fileden.com/files/2006/7/21/139406/AFMergeChange%20bug.swf]Direct link to SWF file of video[/url]

if (!event.willCommit){// Get everything that the user has entered so farvar value = AFMergeChange(event); // If user attempts to enter an invalid sequence of characters, reject itevent.rc = AFExactMatch(/\d*/, value);}

I originally just pasted ^^code^^ into the keystroke code area. But it would disappear every time I closed my script editor. The script was still "there" but it wouldn't be visible anywhere in the document, yet still work as if it was. I've documented it using captivate, and have filed it as a bug. My workaround is to make it a function like you did, and then just call the function in keystroke.

if (!event.willCommit) digits_only_ks();
Dimitri
Expert
Registered: Nov 1 2005
Posts: 1389
Hi nixopax,

It is not a bug but as Adobe puts it "As Designed." Since you are using an AF (Acrobat Forms) function Acrobat thinks it is a custom format function and will not display in the script editor windows. Try checking the "show all scripts" and you should see it there ( which is of course why it still works).
Nice use of Captivate to show the issue clearly!

Hope this helps,

Dimitri
WindJack Solutions
www.windjack.com
www.pdfscripting.com
nixopax
Registered: Feb 6 2009
Posts: 105
When you say "show all scripts" do you mean in Acrobat, or Livecycle? Based on [url=http://www.head4projects.de/adobe/de/adobe/download/head4projects_adobe_livecycle_designer_8_faq.pdf]this update log (See last page of log)[/url] "Show all scripts" appears to be available for Livecycle 8+, but doesn't seem to mention Acrobat at all. I've looked in Acrobat preferences, and various other locations that would seem intuitive to have such a checkbox, but nothing as of yet. Thanks for the heads up that it's not a bug, but rather a "design feature".
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
In Acrobat 9 you'd use "Advanced > Document Processing > Edit All JavaScripts..."
Dimitri
Expert
Registered: Nov 1 2005
Posts: 1389
Hi nixopax

Sorry, should have said Edit All JavaScripts, not Show.... my bad.

Thanks George!

Dimitri
WindJack Solutions
www.windjack.com
www.pdfscripting.com
nixopax
Registered: Feb 6 2009
Posts: 105
That was the very first place I checked once I saw my code disappear.

[url=http://www.fileden.com/files/2006/7/21/139406/AFMergeChangebug2_skin.swf]Link to video player[/url] [url=http://www.fileden.com/files/2006/7/21/139406/AFMergeChangebug2.swf]Direct link to video of me replicating the issue again[/url]
In order to not clutter up my screen with the other code, I made a new file, checked the "All Javascripts". It's empty. Make a form field. Give it the exact code. Did the same thing, disappear. I check the "All Javascripts" again, still empty.

After recording, I tested the field, and it accepted only digits and a backspace, which is exactly what I wanted. Then, I went back to the keystroke code, and just had a //Coment. I checked the Edit All Javascripts, and it showed up. I'm going to fiddle around with this now that I actually get something to show up.

So Dimitri, Adobe may have it "as designed" because it views it as a custom script not to be displayed, but to me, it's a bug not to display anywhere that is editable or even viewable, yet still function.
Dimitri
Expert
Registered: Nov 1 2005
Posts: 1389
Hi nixopax,

Interesting- I think this may be a change in version 9 because the code did show up in earlier versions in the Edit All JavaScripts when we dealt with this exact issue in the past ( can't remember if it was 7 or 8 when it came up before).

Try this as a way to get around it- take all the keystroke script and create a Document Level function then call the Document Level function from the keystroke script.

I agree with you that this is definitely a problem so it's good you reported it as a bug.

Hope this helps,

Dimitri
WindJack Solutions
www.windjack.com
www.pdfscripting.com