Skip to main content
Participant
March 13, 2024
Answered

Formatting phone number with possible extension

  • March 13, 2024
  • 1 reply
  • 1154 views

I'm creating a pdf where I collect business phone numbers and extensions (if applicable) in a field "BusinessPhone" and a field "BusinessExt", then later in the document the two may get combined into "BusinessNumber" if there is anything in the BusinessExt field. 

On Blur for Business Phone I have:

this.getField("BusinessNumber").value = event.target.value;

 

On Blur for BusinessExt I have:

if ((event.target.value) !== "") {

     this.getField("BusinessNumber").value = this.getField("BusinessPhone").valueAsString + " ext. " + this.getField("BusinessExt").valueAsString;

} else {

     this.getField("BusinessNumber").value = this.getField("BusinessPhone").valueAsString;

}

 

Those work for me, but I am having issues with the format of BusinessNumber.  If it is just the BusinessPhone value I want it to show as (999) 999-9999 and if it is BusinessPhone + BusinessExt I want it to show as (999) 999-9999 ext. [as many numbers as are filled in for the extension]. 

 

The phone number may be entered with or without () or - so I want to make all phone numbers look uniform.  I use the format Special > Phone Number on all other phone number fields, and would like this one to look the same.  I'm not very strong in javascript, and have been able to google my situation to learn most of what I want to do, but this one keeps putting me into a dead end.  

 

I'm guessing there is something I can put in Custom Formatting but have not yet found anything that works for me.  I appreciate any advice you can give.  Thank you!

This topic has been closed for replies.
Correct answer Nesa Nurani

Remove all of your scripts (and field format for "BusinessNumber") then use this script as custom calculation script of "BusinessNumber" field:

var bPhone = this.getField("BusinessPhone").valueAsString;
var bExt = this.getField("BusinessExt").valueAsString;

var bPhoneNum = "";
var bExtNum = "";

if(bPhone !== "")
 bPhoneNum = util.printx("(999) 999-9999",bPhone);
if(bExt !== "" && bPhone !== "")
 bExtNum = "ext."+bExt;
event.value = bPhoneNum+bExtNum;

1 reply

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
March 14, 2024

Remove all of your scripts (and field format for "BusinessNumber") then use this script as custom calculation script of "BusinessNumber" field:

var bPhone = this.getField("BusinessPhone").valueAsString;
var bExt = this.getField("BusinessExt").valueAsString;

var bPhoneNum = "";
var bExtNum = "";

if(bPhone !== "")
 bPhoneNum = util.printx("(999) 999-9999",bPhone);
if(bExt !== "" && bPhone !== "")
 bExtNum = "ext."+bExt;
event.value = bPhoneNum+bExtNum;
KNic9903Author
Participant
March 14, 2024

Thank you so much!  This did exactly what I wanted!