Jump to content

Formatting Phone Numbers


Admin1676454018

Recommended Posts

Instructions:

This Phone Formatting Rule can accept any US format (barring major user error) and style it to conform to design guidelines.

If the user enters a phone number that does not meet any traditional US styles, it will be returned back into the template just as the user entered it.

 

Here is a small list of the numbers that it can accept:

847-6608 or 847.6608 or 847*6608

858-847-6608 or 858.847.6608 or (858)847-6608 or (858)-847*6608

1-858-847-6608 or 1.858.847.6608 or 1(858)847-6608 or 1-(858)-847*6608 or +1 (858)8476608

any of the above numbers with extensions using x, ext, ext., extension, no, number, or #

 

Attachments:

1. JavaScript in a txt file

2. Sample template with Javascript Phone Formatting and Roll-up Rules (Note, the Rule in the sample may not be the most current, please use the attached text file for the Script)

 

End User Instructions:

1. create a new DL Rule and copy and paste the attached JavaScript from the text file into the Rule.

2. point the Rule to a variable by modifying the "Field("phoneNumber")" (line 7 below) where phoneNumber is the variable for the phone field.

3. change the format styles to look any way needed (commas, dashes, dots, parens, etc. (lines 1 to 6 below), eg. var formatStyle04 = "$1.$2.$3 ext.$4" will look like 858.847.6600 ext.608.

4. insert the Rule where you want the Phone Number to appear with the formatting applied.

 

 

*************************************************************************************

************************This is what the user will see to edit***********************

*************************************************************************************

 

var formatStyle01 = "$1.$2"; //simple 7 digit phone, could look like $1-$2

var formatStyle02 = "$1.$2.$3"; //simple 10 digit phone, could look like ($1) $2-$3

var formatStyle03 = "+$1 $2.$3.$4"; //10 digit phone starts with 1

var formatStyle04 = "$1.$2.$3 ext.$4"; //10 digit phone with extension

var formatStyle05 = "+$1 $2.$3.$4 ext.$5"; //10 digit phone starts with 1 with extension

var formatStyle06 = "$1.$2 ext.$3"; //7 digit phone with extension

 

var thisNumber = Field("phoneNumber");

 

*************************************************************************************

******************************************end****************************************

*************************************************************************************

Copy and paste everything below this line:

 

 

// This rule allows the user to enter a phone number in any format the want and then have FusionPro re-format

// the number according to your preferences.

 

// To define the separator characters (between the area code, exchange, and number) simply type the character

// between the $1, $2, and $3 in the first six lines of code. By default, the separattor character is a decimal (.).

 

 

var formatStyle01 = "$1.$2"; //simple 7 digit phone

var formatStyle02 = "$1.$2.$3"; //simple 10 digit phone

var formatStyle03 = "$1.$2.$3.$4"; //10 digit phone starts with 1

var formatStyle04 = "$1.$2.$3 ext.$4"; //10 digit phone with extension

var formatStyle05 = "$1.$2.$3.$4 ext.$5"; //10 digit phone starts with 1 with extension

var formatStyle06 = "$1.$2 ext.$3"; //7 digit phone with extension

var formatStyle07 = "$1.$2"; //7 digit phone can end in non-digits

var formatStyle08 = "$1.$2 ext.$3"; //7 digit phone can end in non-digits with extension

var formatStyle09 = "$1.$2.$3"; //7 digit phone can end in non-digits starts with 1

var formatStyle10 = "$1.$2.$3 ext.$4"; //7 digit phone can end in non-digits with extension starts with 1

 

var thisNumber = Field("Phone1");

//////////////////////////////////////////////////////////////////////////////////////////////////////

// DO NOT EDIT BELOW THIS LINE ///////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////

return formatNumber(Trim(thisNumber));

function formatNumber(number01){

var pattern01 = /^(\d{3})[^\w]*(\d{4})$/; // 2201727 or 220-1727 or 220- 1727

var pattern02 = /^[\D]*(\d{3})[\D]*(\d{3})[\D]*(\d{4})$/; // 8002201727 or 800-220-1727 or (800)220-1727 or (800) 220-1727

var pattern03 = /^\+?(\d{1})[\D]*(\d{3})[\D]*(\d{3})[\D]*(\d{4})$/; // 18002201727 or 1-800-220-1727 or +1 (800) 220-1727

var pattern04 = /^[\D]*(\d{3})[\D]*(\d{3})[\D]*(\d{4})\D*[x#n]\D*(\d+)$/; // 800-220-1727 ext 12345 or (800) 220-1727 ext 12345

var pattern05 = /^\+?(\d{1})[\D]*(\d{3})[\D]*(\d{3})[\D]*(\d{4})\D*[x#n]\D*(\d+)$/; // 1-800-220-1727 ext 12345 or +1 (800) 220-1727 ext 12345

var pattern06 = /^(\d{3})[\D]*(\d{4})\D*[x#n]\D*(\d+)$/; // 2201727 ext 1234 or 220-1727 ext 1234 or 220- 1727 ext 1234

 

var pattern07 = /^(\d{3})[^\d]*(\w{7})$/; // 800LETTERS or 800-LETTERS or 800- LETTERS

var pattern08 = /^(\d{3})[\D]*(\w{7})\D*[x#n]\D*(\d+)$/; // 800LETTERS ext 1234 or 800-LETTERS ext 1234 or 800- LETTERS ext 1234

var pattern09 = /^\+?(\d{1})[\D]*(\d{3})[^\d]*(\w{7})$/; // 1800LETTERS or 1-800-LETTERS or +1 (800) LETTERS

var pattern10 = /^\+?(\d{1})[\D]*(\d{3})[^\d]*(\w{7})\D*[x#n]\D*(\d+)$/; // 1-800-LETTERS ext 12345 or +1 (800) LETTERS ext 12345

var patternEndExt = /(.)[x#n](.)/;

var patternStart1 = /^[\D]*[1]/;

if(number01.match(pattern01)){

number01 = number01.replace(pattern01, formatStyle01);

return number01;

} else if(number01.match(pattern02)){

number01 = number01.replace(pattern02, formatStyle02);

return number01;

} else if(number01.match(pattern03)){

if (number01.match(patternStart1)){

number01 = number01.replace(pattern03, formatStyle03);

return number01;

} else {

number01 = number01.replace(pattern06, formatStyle06);

return number01;

}

} else if(number01.match(pattern04)){

number01 = number01.replace(pattern04, formatStyle04);

return number01;

} else if(number01.match(pattern05)){

number01 = number01.replace(pattern05, formatStyle05);

return number01;

} else if(number01.match(pattern06)){

number01 = number01.replace(pattern06, formatStyle06);

return number01;

} else if(number01.match(pattern07)){

number01 = number01.replace(pattern07, formatStyle07);

return number01;

} else if(number01.match(pattern08)){

if(number01.match(patternStart1)){

number01 = number01.replace(pattern10, formatStyle10);

return number01;

} else {

number01 = number01.replace(pattern08, formatStyle08);

return number01;

}

} else if(number01.match(pattern09)){

number01 = number01.replace(pattern09, formatStyle09);

return number01;

} else if(number01.match(pattern10)){

number01 = number01.replace(pattern10, formatStyle10);

return number01;

} else {

//return "no match any pattern";

return number01;

}

}

Link to comment
Share on other sites

  • 3 months later...

I have a client that wants their Direct, Cell and Fax fields formatted with the periods (no problem), but if they choose NexTel they want it formatted: 000*0000*000. I suggested that we create a new drop-down for the Nextel, so that I could format that specific field, but they are not happy with that.

Each phone number has a label before it, so I was wondering if I could say:

if Field 'PhoneLabel1' = NexTel then format Phone1 ....

Any suggestions.

 

They also do not want anything to show if they enter text (letters). Is that possible?

Link to comment
Share on other sites

What about our non US friends? Here is one example. +52-55-5555-5555 Any way to change these dashes to periods? I realize we probably can not account for every format but could we add some sort of 'find and replace' script to the phone rule that would look for certain defined characters, say dashes, and replace them all with another defined character, a period, and get +52.55.5555.5555.
Link to comment
Share on other sites

  • 1 month later...
What about our non US friends? Here is one example. +52-55-5555-5555 Any way to change these dashes to periods? I realize we probably can not account for every format but could we add some sort of 'find and replace' script to the phone rule that would look for certain defined characters, say dashes, and replace them all with another defined character, a period, and get +52.55.5555.5555.

If all you want to do is 'find and replace', that's what the built-in ReplaceSubstring function is for:

var num = "+52-55-5555-5555";
return ReplaceSubstring(num, "-", ".");

Or with regular expressions:

return Field("Phone").replace(/-/g, ".");

I have a client that wants their Direct, Cell and Fax fields formatted with the periods (no problem), but if they choose NexTel they want it formatted: 000*0000*000. I suggested that we create a new drop-down for the Nextel, so that I could format that specific field, but they are not happy with that.

You can always do a replace like my example above on the result of the stock template rule if you want different delimiters along with the other formatting. For instance:

return ReplaceSubstring(Rule("Change phone format Rule"), ".", "*");

Or you can convert the rule to JavaScript and edit the formats.

Each phone number has a label before it, so I was wondering if I could say:

if Field 'PhoneLabel1' = NexTel then format Phone1 ....

Any suggestions.

var num= Rule("Change phone format Rule");
if (Field('PhoneLabel1') == "NexTel")
 return ReplaceSubstring(num, ".", "*");
//else
return num;

 

They also do not want anything to show if they enter text (letters). Is that possible?

Sure, just about anything is possible with JavaScript.

if (num.search(/[a-z]/i)+1)
 return "";

Link to comment
Share on other sites

  • 3 years later...
Looking at all this is very overwhelming but its extremely helpful. Help a novice with javascript. Our client needs a phone format with spaces inbetween. (ex. 123 456 7890). How do I go about doing this?

The easiest way, without digging into all of that complex JavaScript, is probably just to create the default "Change phone format rule" and choose the second format (123-456-7890), then create a second rule which replaces the dashes from that rule with spaces, like so:

return ReplaceSubstring(Rule("Change phone format Rule"), "-", " ");

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...