spotless7325 Posted October 24, 2012 Share Posted October 24, 2012 Hello I have a problem regarding comma after text. My client wants a comma after the telephone number if the variable field after telephone (which are Mobile in this case) are used and so on for the fax field. I have a rule for inserting a comma, but this rule inserts a comma regardless if i writes or not in the next variable field. I want something like this if all variables are used "Phone 044-34 34 34, Mobile 0733-22 22 22, Fax 044-33 22 11" And like this if only Phone are used "Phone 044-34 34 34" Best regards Mattias Quote Link to comment Share on other sites More sharing options...
jwhittaker Posted October 24, 2012 Share Posted October 24, 2012 Mattias I'm sure Dan can give you a 1-liner for this but you can use a if-then statement. this is for the first comma after phone: if ((Field(phone) != "") && ((Field(mobile) != "") || (Field(fax) != ""))) return ", "; else return ""; for the second comma if ((Field(phone) != "") && (Field(mobile) != "") && (Field(fax) != "")) return ", "; else return ""; Quote Link to comment Share on other sites More sharing options...
step Posted October 24, 2012 Share Posted October 24, 2012 I think this might work for you: var result = [Field("phone"),Field("mobile"),Field("fax")]; for(var i=0; i<3; i++){ if (result[i] == "") { result.splice(i,1); i--; } } return result.join(", "); Quote Link to comment Share on other sites More sharing options...
dreimer Posted October 24, 2012 Share Posted October 24, 2012 This forum is great, I learn ways to do things much easier from here. Question in regards to this. What if the data didn't contain the tag, such as "Phone" or "Mobile". Is there a way to have this rule work with hardcoded tags, but suppress if the field is empty and still add the commas as well? Or would it just be easier to change the input data? Quote Link to comment Share on other sites More sharing options...
dreimer Posted October 24, 2012 Share Posted October 24, 2012 Also, with that rule joining fields. Would there be a way to write it for a string of fields but have the last instance of variable found say ", and"? For example: "1, 2, 3, 4, and 5" Quote Link to comment Share on other sites More sharing options...
step Posted October 24, 2012 Share Posted October 24, 2012 In regards to your first question, Try this: var contact = [["phone:",Field("phone")],["mobile:",Field("mobile")],["fax:",Field("fax")]]; var result = []; for(var i=0; i<3; i++){ if (contact[i][1] != "") { result.push(contact[i].join(" ")); } } return result.join(", ") In regards to your second question: if (!Array.prototype.filter) { Array.prototype.filter = function(fun /*, thisp */) { "use strict"; if (this == null) throw new TypeError(); var t = Object(this); var len = t.length >>> 0; if (typeof fun != "function") throw new TypeError(); var res = []; var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in t) { var val = t[i]; // in case fun mutates this if (fun.call(thisp, val, i, t)) res.push(val); } } return res; }; } var contact = [["phone:",Field("phone")],["mobile:",Field("mobile")],["fax:",Field("fax")]]; var result = []; for(var i=0; i<3; i++){ if (contact[i][1] != "") { result.push(contact[i].join(" ")); } } return result.join(", ").replace(/^(.*)(, )(.*?)$/, "$1 and $3"); Quote Link to comment Share on other sites More sharing options...
dreimer Posted October 24, 2012 Share Posted October 24, 2012 Outstanding, Thank You!!!! Quote Link to comment Share on other sites More sharing options...
dreimer Posted October 24, 2012 Share Posted October 24, 2012 step, one the second code to do the string, my client would like it to say for example: 1, 2, 3, and 4. They want the comma before the "and" as well. I updated the code you supplied to accomplish this, but how can I get it to exclude that comma if I have only two of the fields with data, for example 1 and 2. Thanks!!! Quote Link to comment Share on other sites More sharing options...
step Posted October 24, 2012 Share Posted October 24, 2012 Edit the return line: return (result.length>2) ? result.join(", ").replace(/^(.*)(, )(.*?)$/, "$1, and $3") : result.join(", ").replace(/^(.*)(, )(.*?)$/, "$1 and $3"); Quote Link to comment Share on other sites More sharing options...
dreimer Posted October 24, 2012 Share Posted October 24, 2012 Perfect, Thank You! Quote Link to comment Share on other sites More sharing options...
spotless7325 Posted October 29, 2012 Author Share Posted October 29, 2012 Hello Thanks for the quick answers (jwhittaker and step) The one liner from step worked perfect except that i need to put in the text "Fax" in front of the variable for Fax if they use it. Or else you can mistake Phone number and Fax. Like this 040-22 33 44, 0733-33 44 55, Fax: 040-77 22 22 Best regards Quote Link to comment Share on other sites More sharing options...
step Posted October 29, 2012 Share Posted October 29, 2012 Does the code that I wrote for dreimer not solve your problem? Quote Link to comment Share on other sites More sharing options...
spotless7325 Posted October 29, 2012 Author Share Posted October 29, 2012 Hello I just saw that. It almost works (i removed the tags for phone and mobile and kept it for fax), but the rule puts in commas between all variables even if it is empty. If i put in data in every variable it turns out like this 040-11 11 11, 0708-22 22 22, Fax: 040-33 33 33 If i put in data only in the Fax field it turns out like this , , Fax: 040-33 33 33 Here are my slightly modified code var contact = [[Field("Telefon")],[Field("Mobil")],["Fax:",Field("Fax")]]; var result = []; for(var i=0; i<3; i++){ if (contact[1] != "") { result.push(contact.join(" ")); } } return result.join(", ") Best regards Mattias 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.