• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

445

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. 

Votes

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
Community Expert ,
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;

Votes

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
Community Expert ,
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;

Votes

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 

Votes

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
Community Expert ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

Where does you use the script?

Votes

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"

Votes

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
Community Expert ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

What values does you use at the dropdowns?

Votes

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

Votes

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
Community Expert ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

Does you use spaces as entries in the dropdowns?

Votes

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 "

Votes

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
Community Expert ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

So you have no empty fields.

Votes

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"

Votes

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
Community Expert ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

How does you create the empty fields?

Votes

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

Votes

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
Community Expert ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

 Replace

if (val != "") {

with

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

Votes

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. 

Votes

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
Community Expert ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

At the end you can use:

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

 

Votes

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.

Votes

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
Community Expert ,
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

Votes

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
Community Expert ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

----

Votes

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