Skip to main content
Known Participant
April 17, 2019
Answered

Three text boxes but only one required to be completed, if one completed the other 2 are optional

  • April 17, 2019
  • 1 reply
  • 652 views

I have 3 text boxes all numerical values only, EXPRESS TOTAL, ECONOMY TOTAL & SP SVC, I would like at least one of these text boxes to be completed with a value greater than 0. If one box has information then the other 2 textboxes are not required to be completed

I started with the below as a custom calculation script for each of the text boxes but am unsure if i am doing this correctly

Also should the form when opened have all these text boxes as required ticked at source and only when one of these 3 text boxes is completed with a value greater than 0  the required state for the other 2 textboxes will be removed

var aF = ["ECONOMY TOTAL", "SP SVC"]; var sf = this.getField("EXPRESS TOTAL"); for (var i = 0; i < aF.length; i++) {     var v = this.getField(aF).value;     if (v !== "") {         this.getField("last 4 digits").required         break;     } }

but have to admit got lost as to the best way of achieving what i want

This topic has been closed for replies.
Correct answer Thom Parker

You stated that at least one field is "required", meaning that the "field.required" property is set to true. This is the premise of my answer. And this is exactly what the code does. It sets the required property on all three fields based on what the user enters.   If all 3 fields are empty, then all of them are marked as required. However, if any one of them is filled in, then all required properties are set to false, so that it doesn't matter if either of the other two fields is filled or not.  

I think I know the source of your confusion. You're thinking the field that contains the value has to be required. This isn't true. The required property is only useful when fields are not filled in. If a field has a value then it doesn't matter if it is marked as required or not, because it's already filled.

This is a calculation script as I stated in my reply. It doesn't matter what field it is placed in, because all calculation scripts are executed any time any field on the form is changed. It is only placed in one field. A good choice is to create a hidden text field for this purpose so the code is isolated.

1 reply

Thom Parker
Community Expert
Community Expert
April 17, 2019

Only one calculation script is needed, since it operates on all of the fields.

Since only one needs to be filled in, they are either all required, or none are required.

try this.

// Get a list of all the fields of interest

var aFldList = ["ECONOMY TOTAL","EXPRESS TOTAL", "SP SVC"].map(function(a){return this.getField(a);});

// Find out if at least one field has a non-zero number

var bRequired = !aFldList.some(function(a){return !isNaN(a.value) && (a.value > 0);});

// Set required value

aFldList.forEach(function(a){a.required = bRequired;});

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Known Participant
April 18, 2019

Hi Thom

I think you misunderstood me or I didn't explain myself properly

I have the 3 text boxes, at least one of the 3 must be completed with a value greater than 0. If one box is completed then the remaining 2 boxes can either be completed or not, I just don't want to end up with a form with all 3 boxes remaining empty

I didn't understand your comment

Since only one needs to be filled in, they are either all required, or none are required. - as this is not the case

I am assuming the code will not do what I require plus I am unsure as to where the code needs to be placed

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
April 18, 2019

You stated that at least one field is "required", meaning that the "field.required" property is set to true. This is the premise of my answer. And this is exactly what the code does. It sets the required property on all three fields based on what the user enters.   If all 3 fields are empty, then all of them are marked as required. However, if any one of them is filled in, then all required properties are set to false, so that it doesn't matter if either of the other two fields is filled or not.  

I think I know the source of your confusion. You're thinking the field that contains the value has to be required. This isn't true. The required property is only useful when fields are not filled in. If a field has a value then it doesn't matter if it is marked as required or not, because it's already filled.

This is a calculation script as I stated in my reply. It doesn't matter what field it is placed in, because all calculation scripts are executed any time any field on the form is changed. It is only placed in one field. A good choice is to create a hidden text field for this purpose so the code is isolated.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often