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

Phone number formatting using javascript

hammer09
Acrobat 9
Registered: May 19 2006
Posts: 15

Hi Thom,
I look forward to seeing you at this year's PDF conference! Question: I have a javascript that I muddled through (with others' help) to force the formatting of a phone number. Currently, I have a custom format script for the field that looks like this:
 
// RegExp input (999-999-9999) for format
var re = /^(\d{3})[-](\d{3})[-](\d{4})$/
if(re.test(event.value) == true)
{
event.value = RegExp.$1 + "." + RegExp.$2 + "." + RegExp.$3;
}
 
and a custom keystroke script that looks like this:
 
// RegExp input (999-999-9999) for format
var re = /^(\d{3})[-](\d{3})[-](\d{4})$/
if(re.test(event.value) == true)
{
event.value = RegExp.$1 + "." + RegExp.$2 + "." + RegExp.$3;
}
 
It is currently taking input of a number in the format of 555-555-5555 and converts it to 555.555.5555. I need to add the functionality to the script so that it will take input of 5555555555 and format it in the same way. I'm not sure where to go about modifying my script to make this happen. Can you help? Thanks!

Chad Chelius
Chelius Graphic Services
chad [at] cheliusgraphicservices [dot] com

My Product Information:
Acrobat Pro 7, Macintosh
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Two solutions

1. Modify the RegEx to make the dashes optional.


/^(\d{3})[-]?(\d{3})[-]?(\d{4})$/


2. Use the util.printx() function instead of a regular expression. This function will take the first 10 digits no matter what is separating them.

Your keystroke script doesn't do anything. Get rid of it.

Thom Parker
WindJack Solutions
[url=http://www.windjack.com]www.windjack.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://partners.adobe.com/public/developer/en/acrobat/sdk/
pdf/javascript/AcroJS.pdf]Acrobat JavaScript Reference[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script