These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Setting the value of an unchecked check box in a form

Renthing
Registered: Oct 24 2008
Posts: 38
Answered

So, Adobe 8 Professional allows you to set the value of the check box when it is checked but not when it is unchecked.

My situation is I have three fields and a check box, where the three fields are numbers that are manually input to the form and I would like the check box to add 3 to the total of the three fields if it is checked, otherwise leave the total alone if it's unchecked.

I've set the checked value to three and it works as I want it to, adding 3 to the total of the three fields, but when it is left unchecked the field where the total should be displayed displays "NaN" instead.

How do I set the unchecked value of the check box?

Here is the code I'm working with as of right now:

(function () {
var a = +getField("Number 1").value;
var b = +getField("Number 2").value;
var c = +getField("Number 3").value;
var d = +getField("CHK BOX_1").value;
event.value = (a + b + c) + d;
})();

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
In Acrobat Forms, not LiveCycle Designer, the unchecked value of an unchecked check box or radio button is "Off", and this is case sensative.

(function () {
var a = +getField("Number 1").value;
var b = +getField("Number 2").value;
var c = +getField("Number 3").value;
var d = +getField("CHK BOX_1").value;
if (d == "Off") d = 0; // if not checked set value to zero
var e = +getField("TOTAL ARMOR CHECK PENALTY").value;
event.value = (a + b + c + d)-e;
})();

George Kaiser

Renthing
Registered: Oct 24 2008
Posts: 38
Tried your suggestion and it's still giving me a "NaN' result when it's unchecked (What does NaN stand for? I did a search in Help and I received no results).

New code:

(function () {
var a = +getField("Number 1").value;
var b = +getField("Number 2").value;
var c = +getField("Number 3").value;
var d = +getField("CHK BOX_1").value;
if (d == "Off") d = 0;
event.value = a + b + c + d;
}());

When I set the value of the Total field as number I get the error message "The value entered does not match the format of the field [ Total ]."
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
NaN stands for "Not a Number".

Since you can not change the value of D after the assignment. just check the value of the field and set it accordingly.

(function () {
var a = Number(this.getField("Number 1").value);
var b = Number(this.getField("Number 2").value);
var c = Number(this.getField("Number 3").value);
if (this.getField("CHK BOX_1").value == "Off")
var d = 0
else var d = Number(this.getField("CHK BOX_1").value);
event.value = a + b + c + d;
}());

George Kaiser

Renthing
Registered: Oct 24 2008
Posts: 38
That worked! Thank you!
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
> var d = +getField("CHK BOX_1").value;The problem is this line. It attempts to convert the string "Off" to a number, which results in the NaN value. This means that the variable d will never be equal to "Off" and your test will always fail. So you could have just left off the + operator off and done this:

(function () {var a = +getField("Number 1").value;var b = +getField("Number 2").value;var c = +getField("Number 3").value;var d = getField("CHK BOX_1").value;if (d == "Off") d = 0;event.value = a + b + c + d;}());

George