Skip to main content
penone
Participating Frequently
May 22, 2019
Question

How can I combine fields but only include a field if it has a value

  • May 22, 2019
  • 1 reply
  • 397 views

I am trying to put together address fields (company, address, address2, city, state, zip code) into 1 field and am using the following script:

event.value = this.getField("shipper_name").value + "\n" + this.getField("shipper_address1").value + "\n" + this.getField("shipper_address2").value + "\n" + this.getField("shipper_city").value + ", " + this.getField("shipper_state").value + " " + this.getField("shipper_postal_code").value;

Right now - if the company has no shipper_address2 it still adds the line to the field so it looks like this:

ABC Company

123 Main Street

Anywhere, CA 90210

What I would like it to look like is:

ABC Company

123 Main Street

Anywhere, CA 90210

How can I accomplish this?

I believe I should use if/then statements but am not too clear how.

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
May 22, 2019

You can do it like this:

event.value = this.getField("shipper_name").value + "\n" + this.getField("shipper_address1").value + "\n";

if (this.getField("shipper_address2").value!="")

    event.value += this.getField("shipper_address2").value + "\n";

event.value += this.getField("shipper_city").value + ", " + this.getField("shipper_state").value + " " + this.getField("shipper_postal_code").value;