Jump to content

Insert comma after text


spotless7325

Recommended Posts

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

Link to comment
Share on other sites

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 "";

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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!!!
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...