PDA

View Full Version : Insert comma after text


spotless7325
October 24th, 2012, 04:13 AM
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

jwhittaker
October 24th, 2012, 07:29 AM
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 "";

step
October 24th, 2012, 07:49 AM
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(",&nbsp;");

dreimer
October 24th, 2012, 08:05 AM
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?

dreimer
October 24th, 2012, 08:24 AM
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"

step
October 24th, 2012, 09:08 AM
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("&nbsp;"));
}

}

return result.join(",&nbsp;")


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("&nbsp;"));
}

}

return result.join(",&nbsp;").replace(/^(.*)(,&nbsp;)(.*?)$/, "$1 and $3");

dreimer
October 24th, 2012, 09:34 AM
Outstanding, Thank You!!!!

dreimer
October 24th, 2012, 10:45 AM
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!!!

step
October 24th, 2012, 11:04 AM
Edit the return line:

return (result.length>2) ? result.join(",&nbsp;").replace(/^(.*)(,&nbsp;)(.*?)$/, "$1, and $3") : result.join(",&nbsp;").replace(/^(.*)(,&nbsp;)(.*?)$/, "$1 and $3");

dreimer
October 24th, 2012, 11:17 AM
Perfect, Thank You!

spotless7325
October 29th, 2012, 04:17 AM
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

step
October 29th, 2012, 07:13 AM
Does the code that I wrote for dreimer not solve your problem?

spotless7325
October 29th, 2012, 07:31 AM
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[i][1] != "") {
result.push(contact[i].join("&nbsp;"));
}

}

return result.join(",&nbsp;")




Best regards
Mattias