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

PDF Script - check boc retruning off value when unchecked

New Here ,
Feb 08, 2020 Feb 08, 2020

   I have the following script... the output calculation displays "Off" for the boxes the are unchecked (see image). The calculation still works in the end; I have a second field referencing the summed hidden field as a hack to get around the "Off" displaying, but have found that browsers can't handle this.

   Two days and I can't work this out. I know it would be simple but I'm Just a cut and past coder still... Can someone point out how to checkl the value of a check box and return 2 if checks and "" if unchecked?

var v1 = this.getField("Check Box4").value;

if (v1 /= "") {
   event.value = this.getField("Level").value
 + this.getField("Check Box4").value
 + this.getField("Check Box5").value
 + this.getField("Check Box6").value
 + this.getField("Check Box7").value;
}

if (isNaN(v1) || v1 === "") {event.value = "0";}

 

Capture.PNG

TOPICS
How to , PDF forms
852
Translate
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 ,
Feb 09, 2020 Feb 09, 2020

You can use something like this:

 

var v1 = this.getField("Check Box4").valueAsString=="Off" ? "" : 2;

Translate
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 ,
Feb 10, 2020 Feb 10, 2020

When either a checkbox or a radio button is unchecked, the value is always "Off". So, when checkboxes are being added together for a numerical value, the addition has to be qualified. As Try67 has so generously shown in his post. 

 

However, there are some additional issuew with the code, v1 will never be blank, i.e. "", instead the code needs to test for "Off".  Also the "/=" operator is for dividing a value. I think you meant "!=", the inequality operator. 

 

Here's an updated version of the code. I've used 1 as the assumed value of the checkboxes. This will need to be changed to the real value being exported form the checkbox.

 

var v1 = this.getField("Check Box4").value;

if (v1 != "Off") {
   event.value = this.getField("Level").value
 + (this.getField("Check Box4").value=="Off"?0:1)
 + (this.getField("Check Box5").value=="Off"?0:1)
 + (this.getField("Check Box6").value=="Off"?0:1)
 + (this.getField("Check Box7").value=="Off"?0:1);
}
else
{event.value = "0";}

 

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

Translate
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 ,
Feb 10, 2020 Feb 10, 2020

I ended up figuring out what seems to be a very ineligant solution, but it works.  I will try yours. ty 

 

var v0 = this.getField("Level").value; 
var v1 = this.getField("Check Box4").value;
var v2 = this.getField("Check Box5").value;
var v3 = this.getField("Check Box6").value;
var v4 = this.getField("Check Box7").value;

if (v1 != 2) { event.value = "";}
else if(v1 == 2 & v2 == 2 & v3 == 2 & v4 == 2) {event.value = 8 + v0;}
else if(v1 == 2 & v2 == 2 & v3 == 2 & v4 !== 2) {event.value = 6 + v0;}
else if(v1 == 2 & v2 == 2 & v3 !== 2 & v4 !== 2) {event.value = 4 + v0;}
else if(v1 == 2 & v2 !== 2 & v3 !== 2 & v4 !== 2) {event.value = 2 + v0;}
else if(v1 == 2 & v2 !== 2 & v3 == 2 & v4 !== 2) {event.value = "x";}
else if(v1 == 2 & v2 !== 2 & v3 !== 2 & v4 == 2) {event.value = "x";}
else if(v1 == 2 & v2 == 2 & v3 !== 2 & v4 == 2) {event.value = "x";}
else if(v1 == 2 & v2 !== 2 & v3 == 2 & v4 == 2) {event.value = "x";}

 

Translate
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 ,
Feb 10, 2020 Feb 10, 2020

Yep, that code seems like the very long way around 😉

 

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

Translate
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 ,
Feb 10, 2020 Feb 10, 2020

the benefot thou is that it returns 'X' when there is a check box order error as required in my protocol. As in, the series of check boxex should only be checked from lower to highest, any other combination result in "" or "X".

 

Translate
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 ,
Feb 10, 2020 Feb 10, 2020
LATEST

That requirment wasn't mentioned in the first post. If there is an ordering issue, then there needs to be an ordering element to the calculation. Best way to do this in a generic way is to use a loop.

 

 

var nSum = this.getField("Level").value;
var bTestErr = false; // State variable
for(var i=4;i<8;i++)
{
     val = this.getField("Check Box" + i).value=="Off"?0:2;
     if(bTestErr && val)
     {
         nSum = "X";
         break;
      }
      else if(val)
         nSum += val;
      else // any checks beyond this point are not allowed
        bTestErr = true;
}
event.value = nSum;

 

I did not add in the "if" check for the first checkbox. This is just the straight calculation. 

 

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

Translate
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