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

Counting Fields: Dropdown & Radio button field is always counted as filled

Engaged ,
May 04, 2019 May 04, 2019

Copy link to clipboard

Copied

I want to display number of fields filled.... everything is working correct but the dropdown field is being counted as filled already even before it is been selected with a value;

var totalfields = new Array(Text1, Text2, Text3, Text4, Dropdown1, Dropdown2);

var count = 0;

for (i = 0; i < totalfields.length; i++) {

if (this.getField(aFields).valueAsString != "") count++

}

event.value = count;

The above script displays  2 always  i.e. it presumes Dropdown are filled beforehand .

Same is the case with Radio buttons... it is also showing as filled before selecting any option.

Any solution so that it should show 0.

Thanks

TOPICS
Acrobat SDK and JavaScript , Windows

Views

531

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

correct answers 1 Correct answer

Community Expert , May 04, 2019 May 04, 2019

It's not good practice to hard-code values into your code.

Replace this line:

if (this.getField(aFields).valueAsString != "" && this.getField(aFields).valueAsString != "-Select-" && this.getField(aFields).valueAsString != "Off" ) count++

With this:

var f = this.getField(aFields);

if (f.valueAsString != f.defaultValue) count++

Votes

Translate

Translate
Community Expert ,
May 04, 2019 May 04, 2019

Copy link to clipboard

Copied

Before the if statement add this:

console.println(aFields + ": '" + this.getField(aFields).valueAsString + "'");

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
Engaged ,
May 04, 2019 May 04, 2019

Copy link to clipboard

Copied

var aFields = new Array(Text1, Text2, Text3, Text4, Dropdown1, Dropdown2);

var count = 0; // variable to store count of non-empty fields

for (i = 0; i < aFields.length; i++)

{console.println(aFields + ": '" + this.getField(aFields).valueAsString + "'");

if (this.getField(aFields).valueAsString != "") count++

} // end loop

event.value = count;

Still Not working?? it still show 2 dropdown field filled

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 ,
May 04, 2019 May 04, 2019

Copy link to clipboard

Copied

What can you see in the console?

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
Engaged ,
May 04, 2019 May 04, 2019

Copy link to clipboard

Copied

ReferenceError: aFields is not defined

1:Console:Exec

undefined

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
Engaged ,
May 04, 2019 May 04, 2019

Copy link to clipboard

Copied

when I put the below script in the console

var aFields = new Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25);

var count = 0;

for (i = 0; i < aFields.length; i++) {    console.println(aFields + ": '" + this.getField(aFields).valueAsString + "'");

if (this.getField(aFields).valueAsString != "") count++

}

event.value = count;

IT GIVES  "25"

but the the Radio field and Dropdown field is counted as filled even before selecting

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
Engaged ,
May 04, 2019 May 04, 2019

Copy link to clipboard

Copied

Console gives something like this:

1: ''

2: ''

3: ''

4: ''

5: ''

6: ''

7: ''

8: ''

9: ''

10: ''

11: ''

12: ''

13: ''

14: ''

15: ''

16: ''

17: ''

18: ''

19: ''

20: ''

21: '-Select-'

22: ''

23: ''

24: ''

25: 'Off'

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 ,
May 04, 2019 May 04, 2019

Copy link to clipboard

Copied

Looks like that the dropdown gives the value "-Select-" and the unselected radio box the value "Off".

For the field names use this:

var aFields = new Array("Text1", "Text2", "Text3", "Text4", "Dropdown1", "Dropdown2");

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
Engaged ,
May 04, 2019 May 04, 2019

Copy link to clipboard

Copied

Thanks for your help it is working now.

Below is the script. plz suggest any better way (if any):

var aFields = new Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25);

var count = 0; // variable to store count of non-empty fields

for (i = 0; i < aFields.length; i++) {     

if (this.getField(aFields).valueAsString != "" && this.getField(aFields).valueAsString != "-Select-" && this.getField(aFields).valueAsString != "Off" ) count++

} // end loop

event.value = count;

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 ,
May 04, 2019 May 04, 2019

Copy link to clipboard

Copied

It's not good practice to hard-code values into your code.

Replace this line:

if (this.getField(aFields).valueAsString != "" && this.getField(aFields).valueAsString != "-Select-" && this.getField(aFields).valueAsString != "Off" ) count++

With this:

var f = this.getField(aFields);

if (f.valueAsString != f.defaultValue) count++

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
Engaged ,
May 04, 2019 May 04, 2019

Copy link to clipboard

Copied

LATEST

Try67 sir,

Thank for the suggestion, it has made code simple and saved lot of time.

Thanks once again.

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