Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

MAking text fields required using JavaScript

Avatar

Former Community Member
I have several radio button groups where, if one of the buttoms is selected, a text box becomes visible. I would like to kame these text boxes required. I am brand new at this and it took me several days to figure out that in order to make the presence = "visible" work I had to save the form as an xdp file. I am way behind on this project. Can anyone tell me how to make these text boxes required?



Thanks!



Nelson
28 Replies

Avatar

Level 4
What exactly do you mean by required? "Required" usually means that the field must be filled out before the form is submitted, is this what you mean?



Thom Parker

WindJack Solutions


www.windjack.com

Avatar

Former Community Member
You can make text boxes required by selecting them, going to the Object palette's Value tab and setting the Type property to
User Entered - Required.



Stefan

Adobe Systems

Avatar

Former Community Member
Thom -



Yes, that's what I mean. I need to do this using JavaScript because unless the appropriate radio button is checked, the fields will not be required.



Thanks!



Nelson

Avatar

Former Community Member
You can make the fields mandatory using the following javascript:



Textfield1.mandatory = "error";

Textfield1.mandatoryMessage = "This field is required.";



To make the fields optional again



Textfield1.mandatory = "disable";



Note that you will need to change Textfield1 to be the name of the textfield you want to make mandatory.



Denver

Adobe Enterprise Developer Support

Avatar

Former Community Member
I got the scripts to make the fields visible and required, but the execution is extremely slow. The script below takes almost a minute to execute. I have several similar JavaScripts on my form, and they all execute very slowly. I have one summation in FormCalc that executes instantly. Is there some way that I can increase the execution speed of the JavaScripts? All the affected fields are in the same container as the control that executes the scripts.



Thanks!



Nelson

Avatar

Former Community Member
Unfortunately, JavaScript scripts always tend to be slow. If you can write your script in FormCalc, you're wise to do so if you're trying to get better performance. There are cases where you can change a few things around in JavaScript to make your scripts execute faster but if you're scripts are limited to making fields mandatory and optional, you're best bet is to write those scripts in FormCalc.



Here's an example of a script that toggles a text field named TextField1 between mandatory and optional on a button's Click event in FormCalc:



if (TextField1.mandatory == "disabled") then

TextField1.mandatory = "error"

TextField1.mandatoryMessage = "mandatory field!"

else

TextField1.mandatory = "disabled"

endif


Stefan

Adobe Systems

Avatar

Former Community Member
Thanks!



What's the FormCalc script for making a field visible or invisible?



Nelson

Avatar

Former Community Member
This FormCalc script makes a field invisible (still affects layout and can still be referenced from the Acrobat Object Model):



TextField1.presence = "invisible"


This one makes hides the field completely (no longer affects layout but can't be referenced from the Acrobat Object Model either -- this may or may not be important to you depending on your form's needs):



TextField1.presence = "hidden"


Finally, this will make the field visible again:



TextField1.presence = "visible"


Stefan

Adobe Systems

Avatar

Former Community Member
Thanks!



I tired the following script in the change event of the radio button group, but got an message that there was a syntax error on line 1 near token "=". What am I doing wrong?



if (this.rawValue == "Yes") then

if (ExemptReftxb.mandatory == "disabled") then

ExemptReftxb.mandatory = "error"

endif

if (ExemptReftxb.presence = "invisible") then

ExemptReftxb.presence = "visible"

endif



else

if (ExemptReftxb.presence = "visible") then

ExemptReftxb.presence = "invisible"

endif

if (ExemptReftxb.mandatory = "error") then

ExemptReftxb.mandatory = "disabled"

endif

endif



Nelson

Avatar

Former Community Member
It's probably because "this" is a JavaScript reserved word, not FormCalc. Try $.rawValue.



Chris

Adobe Enterprise Developer Support

Avatar

Former Community Member
Thanks!



I made the change from "this" to "$", but got the same error message - Syntax error on line 1near token "=" column 28.



This is driving me nuts!



Nelson

Avatar

Former Community Member
In your first two if statements you use == but in the following statements you use = which assigns a value instead of testing for equality.



Change all of your if statements to use ==



Denver

Adobe Enterprise Support

Avatar

Former Community Member
Changing all the "=" in the if tests to "==" worked, but the execution is just as slow as the JavaScript version. Is ther something else I can do to improve performance?



Nelson

Avatar

Former Community Member
Given the script you're using, I don't think there's much more you can do other than using FormCalc over JavaScript. If you say it's not executing any faster than JavaScript then I think Acrobat is the bottle neck at this point. Acrobat's scripting engines are what's interpreting and executing the script.



Stefan

Adobe Systems

Avatar

Level 4
None of the code you've shown so far should have any affect on performance. There's just not enough of it. The problem is with some other feature of the form. Do you have an extrodinary number of fields? Lists with lots of entries? Web service or DB connections?



Thom Parker

WindJack Solutions


www.windjack.com

Avatar

Former Community Member
Yes -



I have over 400 fields. Could that be the problem?



I have just found that making fields mandatory and showing a mandatoryMessage executes very fast, but making fields visible takes forever.



Thanks for sticking with me on this.



Nelson

Avatar

Former Community Member
I forgot another question. I just got an error message in FormCalc while trying to execute the following script that I originally wrote in JavaScript:



OtherPhystxb.mandatory = "open"



The message said that the "open" wasn't valid.



What is the code to use in FormCalc?



Thanks!



Nelson

Avatar

Former Community Member
The choices are disabled, error, warning, whether you use FormCalc or JavaScript.



Setting a field to mandatory will always be quicker than setting it to visible. Setting a field to mandatory just changes a property. When you make a field visible the layout of the PDF needs to change to accomodate the new field. This could be slow if it's a very large form.



Chris

Adobe Enterprise Developer Support