SSU Posted December 15, 2012 Share Posted December 15, 2012 Hello everyone, I am very new to PTI and JavaScript. I am making a business card that needs to have 2 rules for one field. When someone enters there phone number "1234567891" into the phone number field I need that number to be changed to Tel: 1.234.567.8912 How do I go about making a rule that will add the periods and add or removed "Tel:" based on whether or not they decide to fill in the field on the business card. Quote Link to comment Share on other sites More sharing options...
esmith Posted December 15, 2012 Share Posted December 15, 2012 You just need one rule that does both checks. The question is whether when a value is passed, will it always be 11 digits? I'm thinking you need something that can handle multiple options. In this case, I took FusionPro's default phone formatting code and made some modifications based on your requirements: var thisNumber = Field("Phone"); // your phone field here //////////////////////////////////////////////////////////////////////// // DO NOT EDIT BELOW THIS LINE ///////////////////////////////////////// //////////////////////////////////////////////////////////////////////// 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 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; } } return "Tel: " + formatNumber(Trim(thisNumber)); Quote Link to comment Share on other sites More sharing options...
ThomasLewis Posted December 17, 2012 Share Posted December 17, 2012 Here's what I use for most business cards. It allows you to put in a default area code in case the user only enters 7 digits. You could add this function to JavaScript Globals or right at the start of the rule. function fixnum(nval) { var n = nval.replace(/\D/g,""); var ac = "000"; //common areacode, use if 7 digit phone number entered var a = "("; var b = ") "; var c = "-"; if (n.substring(0,1) == "1") n = n.substring(1); if (n.length < 8) n = ac + n; if (n.length > 9) return a + n.substring(0,3) + b + n.substring(3,6) + c +n.substring(6,10); else return ""; } So In your case you would want to change a, b, and c to this: var a = "Tel: 1."; var b = "."; var c = "."; This is what your rule would be if you put the function in globals: return fixnum(Field("Phone Number")); 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.