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

JavaScript for Form Checkboxes

New Here ,
Dec 30, 2020 Dec 30, 2020

I am looking to display some type of confirmation message or action when 2 or more checkboxes in a form have been checked. Each form will be different. For example. 

Checkboxes names:

Checkbox1; Checkbox2; Checkbox3; Checkbox4.

I want to display message or initiate an action like going to a page when Checkbox1 AND 2 AND 3 are selected. 

Thank You

TOPICS
PDF forms
675
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 ,
Dec 30, 2020 Dec 30, 2020

Create a (hidden) text field with the following code as its custom calculation script:

 

var fields = ["Checkbox1", "Checkbox2", "Checkbox3", "Checkbox4"];
if (event.source!=null && fields.indexOf(event.source.name)!=-1) {
	var counter = 0;
	for (var i in fields) {
		var f = this.getField(fields[i]);
		if (f.valueAsString!="Off") counter++;
	}
	if (counter>=2) {
		app.alert("More then two check-boxes are ticked.",3); // or do something else
	}
}
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 ,
Dec 30, 2020 Dec 30, 2020

Hi Try67. Thanks for the reply. I was vague in my initial post and have been playing around a bit and got it to work using the 'App Alert'. I would like a button to initiate a check and to have the pop up something other then a Java Warning Window. Maybe Display a Text Field or an Image or even open a page within the PDF? I think the easiest would to show the Text Field? The number of check boxes may vary. Sometimes 1...Sometimes more than 1. Big picture I would like to modify that 'App Alert' command to a show field command or something equivalent?

My Button Script Currently:

if (this.getField("CheckBox1").value=="Yes" || this.getField("CheckBox2").value=="Yes" || this.getField("CheckBox3").value=="Yes")
{app.alert("You have isolated the component.");} else {app.alert("You have not isolated the component.");}

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 ,
Dec 31, 2020 Dec 31, 2020

To show a field use this:

this.getField("FieldName").display = display.visible;

To make it hidden use this:

this.getField("FieldName").display = display.hidden;

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 ,
Dec 31, 2020 Dec 31, 2020

Thanks Try67. Do you know of a good site that lists commands I can use for other functions. I just realized I need to also count the number of boxes checked and verify a number checked. Like only 3. Or i need an 'If and only if' function?

 

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 ,
Dec 31, 2020 Dec 31, 2020

Use the code I provided above. The "counter" variable will hold the number of checked boxes at the end of the loop.

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 ,
Dec 31, 2020 Dec 31, 2020
LATEST

Thanks Try.

This is what i ended up using. You helped a lot!

2 Scripts

var nSum = 0;var aCkFlds = this.getField("CheckBox").getArray();for(var i=0;i<aCkFlds.length;i++){if(aCkFlds[i].isBoxChecked(0))nSum++;}event.value = nSum;

 

if (this.getField("CheckBox.3").value=="Yes" && this.getField("CheckBox.1").value=="Yes" && this.getField("CheckBox.4").value=="Yes" && nSum=="3") {this.getField("Results").value = "Correct";} else {this.getField("Results").value = "InCorrect";}

 

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