hkenney Posted July 1, 2015 Share Posted July 1, 2015 I'm trying to figure out how to write a phone formatting rule that will allow for this format: 336 876 9876 I was trying this out below but had some issues if an extension was present or if there was no area code. I'm completely in the dark when it comes to coding! HALP:) var str = Field("Phone");//Phone var newStr = str.replace(/\s+/g, ''); return Left(newStr,3) + " " + Left(Right(newStr,6),3) + " " + Right(newStr,4); Thanks in advanced! Hilary Quote Link to comment Share on other sites More sharing options...
step Posted July 1, 2015 Share Posted July 1, 2015 I'm sort of surprised that's not an option in FusionPro's "Change Phone Format" rule. Well I just converted that to JavaScript and made a few edits: var number = "(111)222-3456"; // Your phone field return formatNumber(number); ////////////////////////////////////////////////////////////////////////////////////////////////////// // DO NOT EDIT BELOW THIS LINE /////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// function formatNumber(number01){ var number01 = Trim(number01); 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 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; } } You could put that function in the JavaScript Globals and access it from any rule in your template or you can paste the rule as is into to use it format one number. Quote Link to comment Share on other sites More sharing options...
hkenney Posted July 1, 2015 Author Share Posted July 1, 2015 I was surprised as well. Thanks so much, worked like a charm! Hilary Quote Link to comment Share on other sites More sharing options...
Dan Korn Posted July 1, 2015 Share Posted July 1, 2015 I'm sort of surprised that's not an option in FusionPro's "Change Phone Format" rule. Well I just converted that to JavaScript and made a few edits: You could put that function in the JavaScript Globals and access it from any rule in your template or you can paste the rule as is into to use it format one number. Or you could just pick the first option "123.456.7890" in the "Change phone format Rule", leave it alone, then have another rule call it and replace the dots with spaces, like so: return ReplaceSubstring(Rule("Change phone format Rule"), ".", " "); Quote Link to comment Share on other sites More sharing options...
hkenney Posted July 1, 2015 Author Share Posted July 1, 2015 Cool thanks Dan that seems like a great option too! Hilary Quote Link to comment Share on other sites More sharing options...
step Posted July 1, 2015 Share Posted July 1, 2015 Or you could just pick the first option "123.456.7890" in the "Change phone format Rule", leave it alone, then have another rule call it and replace the dots with spaces, like so: return ReplaceSubstring(Rule("Change phone format Rule"), ".", " "); True – that thought crossed my mind as well. I decided against it though because: It just seemed a little silly to need two rules to format one number. Doing a string replace would remove the dots that abbreviate extensions. Which I guess really depends on whether you want phone numbers with extensions to be formatted like: "123 456 7890 ext.2089" or "123 456 7890 ext 2089" Aside from that, they are doing the exact same thing. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.