Skip to main content
MADink_Designs27
Inspiring
Jan 13, 2020 1:55 PM UTC
Question

Add Integrated Phone Formatting with Javascript

Is there a way to set a text field format to special > phone using Javascript?

I'm trying to find something in the SDK, but haven't seen anything thus far in field properties or field methods...

I didn't know if there was syntax for setAction perhaps...

I know I could use regex, but the integrated phone format works perfectly for my needs.

I simply prefer to create a master script for the forms I develop and run it all from the script in the console.

This topic is closed to new replies. Start a new post to keep the conversation going.

3 replies

Thom Parker
Community Expert
Community Expert
Jan 15, 2020 12:46 AM UTC

The Acrobat Formatting options are actually JavaScript functions, so if you like the phone format, then just copy what they've done. The name of the function is: AFSpecial_Format,

Just run this name in the Console window and it will display the function code. The input to this function is a number that indicates which format to use. Phone numbers are "2".

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
MADink_Designs27
Inspiring
Jan 15, 2020 6:30 PM UTC

In the time between my original post and now, I had come up with using util.printx as per below:

	this.getField("phone").setAction("Format",
		'if(!event.value||event.value == " ") {\n' +
			'event.value = "";\n' +
		'}\n' +
		'else {\n' +
			'event.value = util.printx(\"(999) 999-9999\", event.value);\n' +
		'}'
	);

 I tried running your code verbatim but realized that wasnt working, so I put it in a setAction and came up with:

this.getField("phone").setAction("Format", "AFSpecial_Format(2)");

Is that how you intended it? The only pitfall I'm running into is when I go into properties, it's adding it as a custom format script instead of the native phone format script. The caveat to that is it allows the user to enter extra characters instead of limiting anything but a digit and only allowing 10 numeric characters/phone formatting characters. It does hide all non-allowed characters once tabbed, however.

Both of the scripts I came up with function the same way, but the AFSpecial_Format is one line instead of multiple lines of code.

 

Translate
MADink_Designs27
Inspiring
Jan 13, 2020 2:38 PM UTC

This isn't exactly what I'm looking for, but I did find something about using the printx function and the following as an example:

   var v = "aaa14159697489zzz";
   v = util.printx("9 (999) 999-9999", v);
   console.println(v);

I'm not sure exactly how to insert it, however. I tried using a setAction("Format", aboveCode); but that didn't seem to work. I'm not sure if I need the variable v at all...

Translate