JavaScript for Form Checkboxes
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
}
}
Copy link to clipboard
Copied
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.");}
Copy link to clipboard
Copied
To show a field use this:
this.getField("FieldName").display = display.visible;
To make it hidden use this:
this.getField("FieldName").display = display.hidden;
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Use the code I provided above. The "counter" variable will hold the number of checked boxes at the end of the loop.
Copy link to clipboard
Copied
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";}

