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

New Here ,
Apr 17, 2019

Copy link to clipboard

Copied

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

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

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.

TOPICS
Acrobat SDK and JavaScript

Views

146

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

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

New Here ,
Apr 17, 2019

Copy link to clipboard

Copied

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

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

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.

TOPICS
Acrobat SDK and JavaScript

Views

147

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 17, 2019 0
Adobe Community Professional ,
Apr 17, 2019

Copy link to clipboard

Copied

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;});

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 17, 2019 0
New Here ,
Apr 17, 2019

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 17, 2019 0
Adobe Community Professional ,
Apr 18, 2019

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 18, 2019 0