Go Back   MarcomCentral (PTI) and FusionPro User Communities > Software-Related Talk > The JavaScript Library > Formatting Text

Notices

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old August 22nd, 2008, 08:14 AM
Admin's Avatar
Admin Admin is offline
Administrator
 
Join Date: Oct 2007
Posts: 331
Default Formatting Phone Numbers

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;
}
}
__________________
Forum Administrator
PTI Marketing Technologies, Inc.
Reply With Quote
  #2  
Old December 9th, 2008, 11:33 AM
gabrielleb gabrielleb is offline
Forum n00b
 
Join Date: Sep 2008
Location: Phoenix, AZ
Posts: 5
Default Re: Formatting Phone Numbers

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?
__________________
gabrielle becker
gabrielleb@harrisprinters.com
Reply With Quote
  #3  
Old December 9th, 2008, 01:23 PM
mduval mduval is offline
Forum n00b
 
Join Date: Dec 2008
Location: Worcester, MA
Posts: 2
Default Re: Formatting Phone Numbers

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.
Reply With Quote
  #4  
Old January 30th, 2009, 09:03 AM
kmcleod kmcleod is offline
Forum n00b
 
Join Date: Jan 2009
Location: Lowell, MA
Posts: 4
Question Re: Formatting Phone Numbers

I agree with MDuval. There doesn't seem to be a way of formating international numbers. Any advice? Bueller, Bueller, anyone, anyone??
Reply With Quote
  #5  
Old January 30th, 2009, 10:44 AM
Dan Korn's Avatar
Dan Korn Dan Korn is offline
FusionPro Senior Engineer / Forum Moderator
 
Join Date: Aug 2008
Location: Chicago, IL
Posts: 4,359
Default Re: Formatting Phone Numbers

Quote:
Originally Posted by mduval View Post
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:
Code:
var num = "+52-55-5555-5555";
return ReplaceSubstring(num, "-", ".");
Or with regular expressions:
Code:
return Field("Phone").replace(/-/g, ".");
Quote:
Originally Posted by gabrielleb View Post
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:
Code:
return ReplaceSubstring(Rule("Change phone format Rule"), ".", "*");
Or you can convert the rule to JavaScript and edit the formats.
Quote:
Originally Posted by gabrielleb View Post
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.
Code:
var num= Rule("Change phone format Rule");
if (Field('PhoneLabel1') == "NexTel")
  return ReplaceSubstring(num, ".", "*");
//else
return num;
Quote:
Originally Posted by gabrielleb View Post
They also do not want anything to show if they enter text (letters). Is that possible?
Sure, just about anything is possible with JavaScript.
Code:
if (num.search(/[a-z]/i)+1)
  return "";
__________________
Dan Korn
FusionPro Developer / JavaScript Guru / Forum Moderator
PTI Marketing Technologies | Printable | MarcomCentral
LinkedIn

I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com.

Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs.

This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data.

Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages.

Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance.

Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support.

How To Ask Questions The Smart Way

The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript.

Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5.

return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)});
Reply With Quote
  #6  
Old October 19th, 2012, 02:49 PM
WBG-guy WBG-guy is offline
Junior Community Member
 
Join Date: Sep 2010
Location: Portland, Oregon
Posts: 23
Default Re: Formatting Phone Numbers

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?
Reply With Quote
  #7  
Old October 19th, 2012, 03:45 PM
Dan Korn's Avatar
Dan Korn Dan Korn is offline
FusionPro Senior Engineer / Forum Moderator
 
Join Date: Aug 2008
Location: Chicago, IL
Posts: 4,359
Default Re: Formatting Phone Numbers

Quote:
Originally Posted by WBG-guy View Post
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:
Code:
return ReplaceSubstring(Rule("Change phone format Rule"), "-", " ");
__________________
Dan Korn
FusionPro Developer / JavaScript Guru / Forum Moderator
PTI Marketing Technologies | Printable | MarcomCentral
LinkedIn

I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com.

Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs.

This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data.

Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages.

Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance.

Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support.

How To Ask Questions The Smart Way

The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript.

Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5.

return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)});
Reply With Quote
  #8  
Old October 19th, 2012, 04:22 PM
WBG-guy WBG-guy is offline
Junior Community Member
 
Join Date: Sep 2010
Location: Portland, Oregon
Posts: 23
Smile Re: Formatting Phone Numbers

This is great! Too easy!! Thanks!!!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 07:02 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) 2011, PTI Marketing Technologies™, Inc.