Combine six fields but sometimes the fields might be blank

New Here ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

I'm a novice at best.

 

I have a form with these separate fields crg1, crg2, crg3, crg4, crg5, crg6.  I have another field - charges.  I need to combine all of the crg fields separated by a ",".  I know how to do this with +", "+, but I can not figure out what to do if one of the crg fields are empty.  Crg1 will NEVER be empty, but there is no guarantee that the other crg fields will have a value. I've searched and searched, but all I can't figure this out.  Please help. 

TOPICS
JavaScript, PDF forms

Views

93

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

One more thing.  I've scripted this with the +", "+ and let's say crg1, crg2, crg3 are filled but the rest are not - here is what I get.

"widget,  pony, ice cream sandwich, , , ". Those extra commas are killing me.  I'm sure there is a way to work this but I just can't find it. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

Try this as a "calculation script", in the Total field:

 

var strTotal = "";
for (var i=1; i<7; i++) {
if (this.getField("crg"+i).value.toString().length > 0) {
strTotal = strTotal + this.getField("crg"+i).value.toString() + ",";
}
}
event.target.value = strTotal;

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

Use this:

var strTotal = "";
for (var i=1; i<7; i++) {
  var val = this.getField("crg"+i).valueAsString;
  if (val != "") {
    if (strTotal != "") strTotal = strTotal + ", ";
    strTotal = strTotal + val;
  }
}
event.value = strTotal;

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

This produced a single comma and nothing else 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

Where does you use the script?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

Crg1-6 are drop down boxs containing text.  I want to put the combination of crg1 through 6 (if applicable) into a text filed called "charges"

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

What values does you use at the dropdowns?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

an example would be "violation of city ordnance."  Or "Failure to pay parking fine." Each of the drop down contains a list of charges similar to what's above.  So if I have 3 charges I want "Failure to pay parking fine, Violation of city ordinance, Disorferly conduct", but what I get is "failure to pay parking fine, violation of city ordinance, disorderly conduct, , ," 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

Does you use spaces as entries in the dropdowns?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

No.  Each entry is the exact length of the item with no spaces before or after.  Example "violation of city ordinance" and not "violation of city ordinance "

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

So you have no empty fields.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

There could be up to 5 empty fields.  There must always be crg1, but after that there is no guarantee that each case will have a total of 6 charges.  They could, but some will have 1 charge, others 2, some 4, some 3, some 5, it just depends on the case.  So if a case has one charge and because the field "charges" uses the +", "+ method to combine crg1, crg2, crg3, crg4, crg5, crg6, I would get this "violation of city ordinance, , , , ," but I want to get this "violation of city ordinance"

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

How does you create the empty fields?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

Ah.  I see.  In the drop down there is a value that is just a space. It looks like this " ".  The drop down starts on the space " " and the user uses the drop down to change it IF there is a charge to add.  So yes in reality I see that it's not a null or blank value, it's a space " ".

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

 Replace

if (val != "") {

with

if (val != "" && val != " ") { 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

This works beautifully!  I am so sorry for failing to provide all the information needed to assist.  One last question... is there a way to no matter what add a period to the end of the final string?  Thanks again, you're a life saver. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

At the end you can use:

if (event.value != "") event.value = event.value + ".";

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

LATEST

This worked a treat!  Thank you for all your help and patience.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

A better test for an empty string is to use a regular expression:

https://www.pdfscripting.com/public/Pattern-Matching-with-Regular-Expressions.cfm

 

Scroll down to the section titled "Detecting No Text".  It's towards the bottom of the page

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

----

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines