Go Back   MarcomCentral (PTI) and FusionPro User Communities > Software-Related Talk > The JavaScript Library

Notices

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old October 24th, 2012, 05:13 AM
spotless7325 spotless7325 is offline
Forum n00b
 
Join Date: Jan 2012
Location: Vellinge, Sweden
Posts: 6
Default Insert comma after text

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
Reply With Quote
  #2  
Old October 24th, 2012, 08:29 AM
jwhittaker jwhittaker is offline
Senior Contributor
 
Join Date: Feb 2009
Location: San Diego
Posts: 169
Default Re: Insert comma after text

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 "";
__________________
FusionPro VDP Creator 10.0.26 | MacOS 10.13.4 | Acrobat DC
Reply With Quote
  #3  
Old October 24th, 2012, 08:49 AM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 957
Default Re: Insert comma after text

I think this might work for you:
Code:
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;");
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
Reply With Quote
  #4  
Old October 24th, 2012, 09:05 AM
dreimer dreimer is offline
Senior Contributor
 
Join Date: Oct 2010
Location: Shakopee, MN
Posts: 495
Default Re: Insert comma after text

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?
__________________
Don Reimer - JavaScript Novice

MAC OS 10.14.6 Acrobat DC FP Creator 12.0.1
Reply With Quote
  #5  
Old October 24th, 2012, 09:24 AM
dreimer dreimer is offline
Senior Contributor
 
Join Date: Oct 2010
Location: Shakopee, MN
Posts: 495
Default Re: Insert comma after text

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"
__________________
Don Reimer - JavaScript Novice

MAC OS 10.14.6 Acrobat DC FP Creator 12.0.1
Reply With Quote
  #6  
Old October 24th, 2012, 10:08 AM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 957
Default Re: Insert comma after text

In regards to your first question, Try this:

Code:
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:
Code:
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");
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
Reply With Quote
  #7  
Old October 24th, 2012, 10:34 AM
dreimer dreimer is offline
Senior Contributor
 
Join Date: Oct 2010
Location: Shakopee, MN
Posts: 495
Default Re: Insert comma after text

Outstanding, Thank You!!!!
__________________
Don Reimer - JavaScript Novice

MAC OS 10.14.6 Acrobat DC FP Creator 12.0.1
Reply With Quote
  #8  
Old October 24th, 2012, 11:45 AM
dreimer dreimer is offline
Senior Contributor
 
Join Date: Oct 2010
Location: Shakopee, MN
Posts: 495
Default Re: Insert comma after text

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!!!
__________________
Don Reimer - JavaScript Novice

MAC OS 10.14.6 Acrobat DC FP Creator 12.0.1
Reply With Quote
  #9  
Old October 24th, 2012, 12:04 PM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 957
Default Re: Insert comma after text

Edit the return line:
Code:
return (result.length>2) ? result.join(",&nbsp;").replace(/^(.*)(,&nbsp;)(.*?)$/, "$1, and $3") : result.join(",&nbsp;").replace(/^(.*)(,&nbsp;)(.*?)$/, "$1 and $3");
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
Reply With Quote
  #10  
Old October 24th, 2012, 12:17 PM
dreimer dreimer is offline
Senior Contributor
 
Join Date: Oct 2010
Location: Shakopee, MN
Posts: 495
Default Re: Insert comma after text

Perfect, Thank You!
__________________
Don Reimer - JavaScript Novice

MAC OS 10.14.6 Acrobat DC FP Creator 12.0.1
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 11:39 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2022, Jelsoft Enterprises Ltd.
(c) 2011, PTI Marketing Technologies™, Inc.