• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Add Integrated Phone Formatting with Javascript

Engaged ,
Jan 13, 2020 Jan 13, 2020

Copy link to clipboard

Copied

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.

TOPICS
Acrobat SDK and JavaScript

Views

2.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 13, 2020 Jan 13, 2020

Copy link to clipboard

Copied

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...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 14, 2020 Jan 14, 2020

Copy link to clipboard

Copied

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".

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 15, 2020 Jan 15, 2020

Copy link to clipboard

Copied

LATEST

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.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines