mstanton Posted August 19, 2009 Posted August 19, 2009 Hello, My question is, how can I combine functions within one rule? For example, I want to both format the phone number in the Fax field, AND add the string "Fax:" in front of it. I know how to do each separately, but how do I put them in the same rule? Right now the only part of the JS that it is paying attention to is adding "Fax:" at the beginning. if (Field("Fax") != "") { return ("Fax: " + Field("Fax")); } else return ""; Var1="Fax"; CaseSelection="Format 2"; 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 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 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; } }Thanks!
esmith Posted August 19, 2009 Posted August 19, 2009 Our jobs server has been offline for the past half hour so I took some time to dig through your code. I'm not quite sure what the point of the CaseSelection variable is for, especially since you are writing code that does the same thing regardless of the value. I think what you want to be using is this: if (Field("Fax") != "") { return "Fax: " + formatNumber(Trim(Field("Fax"))); } else return ""; ////////////////////////////////////////////////////////////////////////////////////////////////////// // DO NOT EDIT BELOW THIS LINE /////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// 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; } } Note that when using a function, the value inside the parenthesis (in this case, "number01") is the var name assigned to whatever is passed to the function in the previous code (in this case "Trim(Field("Fax"))"). You do not need to create a unique variable named thisNumber first. Kudos to you for tackling such a complicated rule! I'm still learning how to use functions myself. Don't even get me started on completely digesting all the regular expressions within the function pattern vars above.
mstanton Posted August 19, 2009 Author Posted August 19, 2009 Heheh, well, I didn't really take on anything. There is a preset rule in FusionPro 6 that formats the number for you. All I did was tell it which field I wanted formatted. Then I copy/pasted the code it spat out here It made me look a bit smarter than I am! I will try what you suggested and let you know how it goes. Thanks!
esmith Posted August 19, 2009 Posted August 19, 2009 You know, I'm looking over the code again in light of the knowledge that you converted a FP6 preset rule to JS. I see that the function references several formatStyle## variables, but don't see anywhere where those varaibles are defined. I also see defined variables for patternEndExt and patternStart1 in the function, but don't see where they are used. Can you confirm that all the code from the original script is included in your OP? Without additional information, I'm guessing my code snippet will not validate or return an error... EDIT: Aha! I just saw where those variables are referenced -- in the code that I dumped from your OP! I still don't understand the point of the CaseSelection variable (I'm guessing you specificed which case you preferred in the wizard?) but regardless, the rule will have to be revised from my original suggestion. How about: //Indicate name of field in your data with raw fax number Var1="Fax"; // Choose which of 4 optional format styles you prefer CaseSelection="Format 2"; 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 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); if (Field("Fax") != "") { return "Fax: " + formatNumber(Trim(thisNumber)); } else return ""; } 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); if (Field("Fax") != "") { return "Fax: " + formatNumber(Trim(thisNumber)); } else return ""; } 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); if (Field("Fax") != "") { return "Fax: " + formatNumber(Trim(thisNumber)); } else return ""; } 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); if (Field("Fax") != "") { return "Fax: " + formatNumber(Trim(thisNumber)); } else return ""; } ////////////////////////////////////////////////////////////////////////////////////////////////////// // DO NOT EDIT BELOW THIS LINE /////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// 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; } }
mstanton Posted August 20, 2009 Author Posted August 20, 2009 Thanks, Eric! That worked great. Do you know of a good resource for me to use to learn this stuff? Asking a question on a forum is great, but I want to be able to actually learn this code so I can use it properly.
esmith Posted August 20, 2009 Posted August 20, 2009 I just google "JavaScript" along with whatever I want to know how to do.
Recommended Posts
Archived
This topic is now archived and is closed to further replies.