Fields get concanated instead of adding the numbers

Contributor ,
Feb 23, 2019

Copy link to clipboard

Copied

Hello,

I am facing strange problem across all PDF Forms which i make via javascript.

The number fields instead of getting sum it gets concanated.

Please watch the below video to get clear picture of what is the problem.

Dropbox - concatenation problem.avi

basically i had to get the average of the number of fields filled by user......

The following is the script i used:

var counter = 0;

for (i=11; i<=20; i++) {

    if (/^\s*$/.test(this.getField(i).valueAsString)==true) counter++;

}

if (counter>0) {

    var total = counter;

} else total = "";

var v2 = 10 - total;     // to get the number of filled fileds

var v1 = this.getField("11").value + this.getField("12").value + this.getField("13").value + this.getField("14").value + this.getField("15").value + this.getField("16").value + this.getField("17").value + this.getField("18").value + this.getField("19").value + this.getField("20").value;   // sum of all the fields

event.value = v2 !== 0 ? v1 / v2 : "";    //average = sum/number of filled fields

Thanks in advance

Adobe Community Professional
Correct answer by Bernd Alheit | Adobe Community Professional

Use this:

var v1 = Number(this.getField("11").valueAsString) + Number(this.getField("12").valueAsString) + ...

TOPICS
Acrobat SDK and JavaScript, Windows

Views

183

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

Fields get concanated instead of adding the numbers

Contributor ,
Feb 23, 2019

Copy link to clipboard

Copied

Hello,

I am facing strange problem across all PDF Forms which i make via javascript.

The number fields instead of getting sum it gets concanated.

Please watch the below video to get clear picture of what is the problem.

Dropbox - concatenation problem.avi

basically i had to get the average of the number of fields filled by user......

The following is the script i used:

var counter = 0;

for (i=11; i<=20; i++) {

    if (/^\s*$/.test(this.getField(i).valueAsString)==true) counter++;

}

if (counter>0) {

    var total = counter;

} else total = "";

var v2 = 10 - total;     // to get the number of filled fileds

var v1 = this.getField("11").value + this.getField("12").value + this.getField("13").value + this.getField("14").value + this.getField("15").value + this.getField("16").value + this.getField("17").value + this.getField("18").value + this.getField("19").value + this.getField("20").value;   // sum of all the fields

event.value = v2 !== 0 ? v1 / v2 : "";    //average = sum/number of filled fields

Thanks in advance

Adobe Community Professional
Correct answer by Bernd Alheit | Adobe Community Professional

Use this:

var v1 = Number(this.getField("11").valueAsString) + Number(this.getField("12").valueAsString) + ...

TOPICS
Acrobat SDK and JavaScript, Windows

Views

184

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
Feb 23, 2019 0
Adobe Community Professional ,
Feb 24, 2019

Copy link to clipboard

Copied

Use this:

var v1 = Number(this.getField("11").valueAsString) + Number(this.getField("12").valueAsString) + ...

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
Reply
Loading...
Feb 24, 2019 1
Contributor ,
Feb 24, 2019

Copy link to clipboard

Copied

Thanks That worked...

Thanks a lot

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
Reply
Loading...
Feb 24, 2019 0