PDA

View Full Version : Phone Format Rule


Marc
May 21st, 2013, 10:08 AM
I am trying to get Fusion to return the phone format listed below with dots between numbers however if you type - (dashes) or blank spaces it will return exactly that, not dots like I need it to be.

Main: 403.239.3797 e.4026 (format needs to be like this)

Here is the rule I am using, do not know what I am missing??

Var1="Phone1";
CaseSelection="Format 1";



if(CaseSelection == "Format 1")
{
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 e.$4"; //10 digit phone with extension
var formatStyle05 = "$1 $2.$3.$4 e.$5"; //10 digit phone starts with 1 with extension
var formatStyle06 = "$1.$2 ext.$3"; //7 digit phone with extension

var thisNumber = Field(Var1);

return formatNumber(Trim(thisNumber));
}

Using Fusion Pro V 8.1.2

Thanks
Marc

jwhittaker
May 21st, 2013, 10:17 AM
Marc
You have to keep the rest of the Javascript from the rule

Var1="Phone1";
CaseSelection="Format 1";

if(CaseSelection == "Format 1")
{
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 e.$4"; //10 digit phone with extension
var formatStyle05 = "$1 $2.$3.$4 e.$5"; //10 digit phone starts with 1 with extension
var formatStyle06 = "$1.$2 e.$3"; //7 digit phone with extension

var thisNumber = Field(Var1);

return formatNumber(Trim(thisNumber));
}


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

return formatNumber(Trim(thisNumber));

function formatNumber(number01){

var pattern01 = /^(\d{3})[^\d]*(\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 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 {
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 {
//return "no match any pattern";
return number01;
}
}

Marc
May 21st, 2013, 10:58 AM
Thanks for the response. I do have the rest of the java in the rule. I only pasted format 1. Here is my complete javascript rule. Any suggestions to what I am missing?

Var1="Phone1";
CaseSelection="Format 1";



if(CaseSelection == "Format 1")
{
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 e.$4"; //10 digit phone with extension
var formatStyle05 = "$1 $2.$3.$4 e.$5"; //10 digit phone starts with 1 with extension
var formatStyle06 = "$1.$2 ext.$3"; //7 digit phone with extension

var thisNumber = Field(Var1);

return formatNumber(Trim(thisNumber));
}





if(CaseSelection == "Format 2")
{
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 thisNumber = Field(Var1);

return formatNumber(Trim(thisNumber));
}





if(CaseSelection == "Format 3")
{
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 thisNumber = Field(Var1);

return formatNumber(Trim(thisNumber));
}






if(CaseSelection == "Format 4")
{
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 thisNumber = Field(Var1);

return formatNumber(Trim(thisNumber));
}









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

return formatNumber(Trim(thisNumber));

function formatNumber(number01){

var pattern01 = /^(\d{3})[^\d]*(\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 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 {
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 {
//return "no match any pattern";
return number01;
}
}

jwhittaker
May 21st, 2013, 11:06 AM
What are you putting into the Phone field? Can you give some examples? I copy and pasted and it works for me. You do need to change the last line
var formatStyle06 = "$1.$2 ext.$3"; //7 digit phone with extension
to your format var formatStyle06 = "$1.$2 e.$3";

step
May 21st, 2013, 11:23 AM
The regular expression recognizes an extension by finding an 'x', a '#', or an 'n'. Only passing "e." to indicate an phone number extension is breaking the rule. That being said, you can modify the "Format 1" portion of the 'if' statement like so to get the results you want:


if(CaseSelection == "Format 1")
{
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 e.$4"; //10 digit phone with extension
var formatStyle05 = "$1 $2.$3.$4 e.$5"; //10 digit phone starts with 1 with extension
var formatStyle06 = "$1.$2 ext.$3"; //7 digit phone with extension

var thisNumber = Field(Var1).replace(/e/gi,"ext.");

return formatNumber(Trim(thisNumber));
}


Alternatively, you could edit those search patterns to look for an 'e' (though, it's probably not recommended):

var pattern04 = /^[\D]*(\d{3})[\D]*(\d{3})[\D]*(\d{4})\D*[ex#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*[ex#n]\D*(\d+)$/;
// 1-800-220-1727 ext 12345 or +1 (800) 220-1727 ext 12345
var pattern06 = /^(\d{3})[\D]*(\d{4})\D*[ex#n]\D*(\d+)$/;
// 2201727 ext 1234 or 220-1727 ext 1234 or 220- 1727 ext 1234

Marc
May 22nd, 2013, 07:06 AM
Thank You for your replies, very helpful