Jump to content

Remove only 1 word out of text box when field is empty


prhoward

Recommended Posts

I'm a newbie here and could use some help.

I have a single text box that has the line below

Tel: ### ###-####, Mobile ### ###-####, Fax ### ###-####

I need to suppress the word Mobile if the mobile number field is empty. Right now if I set Suppress if empty it will empty the entire row and suppress if missing variable will only leave the field blank and not remove the word Mobile.

Is it possible to remove the word Mobile or do I need to have the designer create separate text boxes for each?

Thank you for your help

Link to comment
Share on other sites

Welcome to the FP forums, home of the most helpful forum members for a production app that I have ever been involved with. :)

 

You'll probably want to delve into the mysterious yet tame waters of JavaScript to solve your problem. Basically, you will build a custom text rule to return just the information that you have data for, and then return the result of that rule in your text frame rather than a combination of static text and data fields. One way to skin that cat would be to use the following code:

 

var phone = Field("Phone"); //replace with your field
var mobile = Field("Mobile"); //replace with your field
var fax = Field("Fax"); //replace with your field
var result = "";

result += (phone) ? "Tel: " + phone;
result += (mobile) ? ", Mobile: " + mobile;
result += (fax) ? ", Fax: " + fax;
return result;

In this case, the final value is created by only adding the static text and relevant field data for fields that contain content. The only element you place in the variable text frame is the name of this rule (formatted with the appropriate font style in the text frame).

 

Welcome to the land of JavaScript, where if you can imagine it, you can either do it or find someone else who already has. ;)

Link to comment
Share on other sites

I just realized while testing code for another post that I left a piece out on this response. Code above should read (changes in red):

var phone = Field("Phone"); //replace with your field
var mobile = Field("Mobile"); //replace with your field
var fax = Field("Fax"); //replace with your field
var result = "";

result += (phone) ? "Tel: " + phone [color="Red"]: ""[/color];
result += (mobile) ? ", Mobile: " + mobile [color="Red"]: ""[/color];
result += (fax) ? ", Fax: " + fax [color="Red"]: ""[/color];
return result;

Also, the code will need tweaking if you have a mobile and/or fax number, but no phone since the result will begin with a ", ". If this is a problem, You would need to further edit the code to remove the first two characters in this situation like so (again in red):

var phone = Field("Phone"); //replace with your field
var mobile = Field("Mobile"); //replace with your field
var fax = Field("Fax"); //replace with your field
var result = "";

result += (phone) ? "Tel: " + phone : "";
result += (mobile) ? ", Mobile: " + mobile : "";
result += (fax) ? ", Fax: " + fax : "";

[color="Red"]if (Left(result, 1) == ",") result = Right(result, result.length - 2);[/color]
return result;

Link to comment
Share on other sites

Here's a more generalized solution:

var EntriesWithLabels = [
   "Tel", Field("Phone"),
   "Mobile", Field("Mobile"),
   "Fax", Field("Fax"),
];

var resultArray = new Array;
for (var i = 0; i < EntriesWithLabels.length; i += 2)
 if (EntriesWithLabels[i] && EntriesWithLabels[i+1])
   resultArray.push(EntriesWithLabels[i] + ": " + EntriesWithLabels[i+1]);

return resultArray.join(", ");

You can simply add or remove pairs of labels and data fields at the top. The Array.join method takes care of the problem of putting the delimiter (the comma and space in this case) only between items, and not "dangling" at the beginning or end.

This really helped me out! I just had one variable line with an "Ext:". It suppressed the word if the field was left blank and then shifted the type up. Perfect! Thank you!

If each entry is on its own line, then you don't need any JavaScript at all to accomplish this. You can do it all in the Variable Text Editor. Simply type the label and then insert the variable for the data field you want, then click "Paragraph" and check the box for "Suppress if Empty", then change the drop-down box to "Containing Empty Variables". You can see this in the Cell Phone tutorial, in the address box in the middle of the first page, for the "Add2" line.

Edited by Dan Korn
Change to "Suppress if Containing Empty Variables"
Link to comment
Share on other sites

  • 5 years later...
I just realized while testing code for another post that I left a piece out on this response. Code above should read (changes in red):

var phone = Field("Phone"); //replace with your field
var mobile = Field("Mobile"); //replace with your field
var fax = Field("Fax"); //replace with your field
var result = "";

result += (phone) ? "Tel: " + phone [color="Red"]: ""[/color];
result += (mobile) ? ", Mobile: " + mobile [color="Red"]: ""[/color];
result += (fax) ? ", Fax: " + fax [color="Red"]: ""[/color];
return result;

Also, the code will need tweaking if you have a mobile and/or fax number, but no phone since the result will begin with a ", ". If this is a problem, You would need to further edit the code to remove the first two characters in this situation like so (again in red):

var phone = Field("Phone"); //replace with your field
var mobile = Field("Mobile"); //replace with your field
var fax = Field("Fax"); //replace with your field
var result = "";

result += (phone) ? "Tel: " + phone : "";
result += (mobile) ? ", Mobile: " + mobile : "";
result += (fax) ? ", Fax: " + fax : "";

[color="Red"]if (Left(result, 1) == ",") result = Right(result, result.length - 2);[/color]
return result;

 

I'm pretty sure a modification of this code could help me. I have a similar situation with three variables in a text frame. The text frame reads:

"<<V1>> <<V2>> <<V3>>."

 

The result I'm getting when <<V3>> is empty is:

"<<V1>> <<V2>> ."

 

How can I remove the space between "<<V2>> ." without just deleting the static text in the text frame?

Link to comment
Share on other sites

I'm pretty sure a modification of this code could help me. I have a similar situation with three variables in a text frame. The text frame reads:

"<<V1>> <<V2>> <<V3>>."

 

The result I'm getting when <<V3>> is empty is:

"<<V1>> <<V2>> ."

 

How can I remove the space between "<<V2>> ." without just deleting the static text in the text frame?

Assuming you're still trying to make this idea work for you, it's not really as simple as what's in this thread. The great thing about rules is the control they give you over how you want to format a particular string based on the data you have. In the example you've quoted, number descriptors ("Tel:", "Fax:", etc) are added based on whether or not those data fields are populated and then the resulting string is returned. What you're trying to accomplish is setting the format and then attempting to create a rule to re-format it to match the data. In the interest of keeping this thread somewhat on topic, here's an example:

var numbers = {
 'Tel': '123 456 7890',
 'Mobile': '',
 'Fax': '456 555 1230'
};

var result = 'Tel: ### ###-####, Mobile: ### ###-####, Fax: ### ###-####';

for (var i in numbers)
 result = result.replace(i + ': ### ###-####', i + ': ' + numbers[i]);

return result.split(', ').filter(function(s){ return /\d/.test(s);}).join(', ');

 

The alternative (and possibly better) solution is to just replace the entire placeholder string with your rule instead of trying to replace each variable individually:

var numbers = {
 'Tel': '123 456 7890',
 'Mobile': '',
 'Fax': '456 555 1230'
};

var paragraph = 'Contact us today at: Tel: ### ###-####, Mobile: ### ###-####, Fax: ### ###-####';

var placeholder = 'Tel: ### ###-####, Mobile: ### ###-####, Fax: ### ###-####';
var replace = [];
for (var i in numbers)
 if (number = numbers[i])
   replace.push(i + ': ' + number);

return paragraph.replace(placeholder, replace.join(', '));

Link to comment
Share on other sites

  • 2 years later...

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...