Setting another field to read only if current field has a value
Copy link to clipboard
Copied
I have two fields, 401DollarAmt and 401PercentAmt. If 401DollarAmt has a value in it, then 401PercentAmt should be set to read-only.
In the 401DollarAmt field, under the Actions tab, I have "Mouse Up" as the trigger. Then I have this code:
if (event.target.value != “”)
{
401PercentAmt.readonly = true;
}
I'm not sure what I'm doing wrong. I'm uncertain about how to use the event object and when to use the target object.
Copy link to clipboard
Copied
You have a couple of issues there.
-The first one is a syntax one. You can't use "fancy" quotes in your code, only straight single or double-quotes.
- Also, you can't access a field simply by writing it's name. You need to use the getField method.
- In addition, assuming this field is a text field, I would use the Validation event, instead of MouseUp.
- Finally, you'll probably want to set the other field back as not required when this field is empty, so that needs to be added.
This code does all of that:
this.getField("401PercentAmt").readonly = (event.value != "");
Copy link to clipboard
Copied
Wow, that's a lot.
1. The quotes I have are double quotes. What you mean by "fancy" quotes?
2. I see what you mean.
3. How would you use a Validation event? I am guessing that you would pull up the properties window of the textbox, go to the Validate tab, then select the radio button called Run custom validation script. After the user types in a value to the textbox, then exits the textbox, this will trigger a Validation event. When that occurs, any Java Script code in the Run custom validation script area will be executed. Is this correct?
If this is correct, is there any other way to use the Validation event w/o going thru the GUI? If so, how would you use the Validation event programmatically? Can you send any website links?
Copy link to clipboard
Copied
1. Zoom in to the quotes you used and the quotes I used. Do you see a difference?
You should only use a plain-text editor to write and edit code. Don't use something like Word as it tends to insert this kind of quotes automatically when you enter them, and that doesn't work for scripts.
3. Correct.
You can set a validation script programmatically using the Field's setAction method, like this:
this.getField("FieldName").setAction("Validate", "event.rc = true;");
The second parameter is the code to be executed, in String format. But why do you want to do that? Do you want to apply this code to multiple fields?
This is all documented in the Acrobat JavaScript API Reference, which is a part of the Acrobat SDK:
Adobe - Acrobat Developer Center | Adobe Developer Connection

