Copy link to clipboard
Copied
Hello me again
I've got some grouped radio buttons on a form 2 sets 1 set is a yes or no answer and the 2nd set should only be completed if the answer t
Cash Yes No
Cash Controlled Yes No
If the answer to cash is Yes the user must complete CashControlled, if the answer is No they can move on to the next question.
I've got following code in Cash Yes field on mouse up
if (event.target.value == "Yes") {
this.getField("CashControlled").required = true;
}
and in the Cash No field I have
if (event.target.value == "No") {
this.getField("CashControlled").required = false;
}
This works but if a user goes back and selects a different option the fields don't update correctly for example
if the user selects No on Cash and then changes to yes the field doesn't update to required and vice versa
What am I doing wrong?
TIA
Ellen
It is possible to create a document level JavaScript function that would work for both radio buttons and check boxes that performs this task. Each button or check box would have the following "Mouse Up" action:
SetRequiredbyRB("CashControlled");
And then add the following document level function:
function SetRequiredbyRB(cRequiredField) {
var oRequiredField = this.getField(cRequiredField);
var bRequired = false;
function MakeReadOnly() {
bRequired = "Off"; // remove required;
oRequiredField.readonly
...Copy link to clipboard
Copied
two equals sets the value three ask for an exact match.
you need to have === not ==
Personally I prefer to separate definitions and the if statement so that I can reuse it.
I would use this script on both radio buttons on Mouse Release:
var Q = this.getField("Cash");
var A = this.getField("CashControlled");
if (Q.value === "Yes") {
A.required = true;
}
if (Q.value === "No") {
A.required = false;
}
Copy link to clipboard
Copied
Unless one of the radio buttons is set as being checked by default, there exist the situation where nether button has been selected and the value of the group is neither "Yes" nor "No". I would also clear the selection of the "CashControlled" radio button group. When there is no button selected within a radio button group, the value of the group is "Off". The "Cash" field will not have a selection when created or the form is reset unless one of the radio buttons in the group is set to be checked by default;
if (event.target.value == "Yes") {
// Yes is selected;
this.getField("CashControlled").required = true;
} else {
// any other answer than "Yes" included "No" and "Off" (no selected button);
this.getField("CashControlled").required = false;
this.getField("CashControlled").value = "Off";
}
If both buttons run the same code, then the possibility of
Copy link to clipboard
Copied
Hello
Thanks for your help
Does the code get added to the Cash Yes No radio buttons?
TIA
Ellen
Copy link to clipboard
Copied
Yes.
Copy link to clipboard
Copied
https://forums.adobe.com/people/Lukas+Engqvist wrote
two equals sets the value three ask for an exact match.
you need to have === not ==
...
This is not correct. One equal sets the value.
Copy link to clipboard
Copied
yes sorry
you are right
Copy link to clipboard
Copied
It is possible to create a document level JavaScript function that would work for both radio buttons and check boxes that performs this task. Each button or check box would have the following "Mouse Up" action:
SetRequiredbyRB("CashControlled");
And then add the following document level function:
function SetRequiredbyRB(cRequiredField) {
var oRequiredField = this.getField(cRequiredField);
var bRequired = false;
function MakeReadOnly() {
bRequired = "Off"; // remove required;
oRequiredField.readonly = true; // make read only;
oRequiredField.value = ""; // clear value;
return;
}
switch(event.target.value) {
case "Yes":
bRequired = true; // make required;
oRequiredField.readonly = false; // make writable;
oRequiredField.setFocus(); // jump to field;
break;
case "No":
case "Off":
MakeReadOnly();
break;
default:
MakeReadOnly();
// issue warning;
app.alert("Unkown response " + event.target.value + " for " + event.target.value, 1, 0, "Unkonw Response");
break;
} // end switch event.target.value;
oRequiredField.required = bRequired; // set required property;
return bRequired; // return reuired value;
} // end SetREquiredbyRB function;
The above code does not rely on the on using the "==" or "===" comparison operator and can be used with any number of fields that require this type of action by just changing the field name parameter in the called function for the radio button or check box. It also reports if group of buttons has a new selection added and the function is not update for this change.