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

Replace string

Gheezer
Registered: Feb 16 2009
Posts: 19
Answered

Everything I can find tells me that this should work without a hitch, yet it does not update the field when run.

This is suppose to take a number, 1234567 and change it to 123-4567. I have it in the On Blur action.
What am I missing?

if(event.value.length == 8)
{
var s = event.value;

var r = s.substring(0,3) + "-" + s.substring(3);
event.value = r;
}

Troy

My Product Information:
Acrobat Pro 8.1.1, Windows
try67
Online
Expert
Registered: Oct 30 2008
Posts: 2399
The length of your string is 7, not 8.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The correct place to put this script is in either a Custom Keystroke or Format event, depending on how you want the data handled.

Here's a rewrite that uses a custom keystroke script:
if(!event.willCommit)event.rc = (/^\d+$/).test(event.change);elseevent.value = event.value.replace(/(\d{3})(\d{4})/,"$1-$2");

The script only allows digits to be entered and reformats the first 7 when the user presses the enter key or clicks out of field.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

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

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
You can also use the 'util.printf()' method or the RegExp object to format the string with or without the '-' seperator in the custom format script:

 // using the substring methodvar s = event.value;if(s.length == 7){var r = s.substring(0,3) + "-" + s.substring(3);event.value = r;}

// using the util.printf methodif(event.value.length == 7){s8Phone = "%,103d" + "-" + "%,104d";event.value = util.printf(s8Phone, event.value.substring(0,3), event.value.substring(3));}

// using the RegExp objectvar re7PhoneFrmt = /^(\d{3})(\d{4})$/if( re7PhoneFrmt.test(event.value) ) {event.value = RegExp.$1 + "-" + RegExp.$2;}

George Kaiser

Gheezer
Registered: Feb 16 2009
Posts: 19
thomp wrote:
The correct place to put this script is in either a Custom Keystroke or Format event, depending on how you want the data handled.Here's a rewrite that uses a custom keystroke script:
if(!event.willCommit)event.rc = (/^\d+$/).test(event.change);elseevent.value = event.value.replace(/(\d{3})(\d{4})/,"$1-$2");

The script only allows digits to be entered and reformats the first 7 when the user presses the enter key or clicks out of field.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]
Thanks for your reply.

When I place the code in the keystroke script, the backspace and delete keys do not function.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Well, there's that:( Modify the code to accept empty input.
if(!event.willCommit)event.rc = !event.change.length ||  (/^\d+$/).test(event.change);elseevent.value = event.value.replace(/(\d{3})(\d{4})/,"$1-$2");

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

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