Print Page | Close Window

require any 2 of many text fields

Printed From: Foxit's Planet PDF Forum
Category: Lets Talk PDF
Forum Name: JavaScript
Forum Description: Discussions relating to the use of JavaScript within PDF.
Printed Date: 21 Jul 2019 at 10:26am
Software Version: Web Wiz Forums 11.10 -

Topic: require any 2 of many text fields
Posted By: morrison
Subject: require any 2 of many text fields
Date Posted: 13 Oct 2015 at 4:53pm

Document rule:  "Require two answers be supplied as text via javascripting"
Any two but no more than two, of the 19 text fields in this section of the document may be completed.

When all you have is a hammer, everything looks like a nail. So, based on my limited ability, this is what I came up with to check if all the fields were left blank and, if so, require that area.

/*********** belongs to: AcroForm:
t_js_carrier_ranking:Calculate ***********/
var car1 = this.getField("gr_carrier_1_rank")
var car2 = this.getField("gr_carrier_2_rank")
var car3 = this.getField("gr_carrier_3_rank")
var car4 = this.getField("gr_carrier_4_rank")
var car5 = this.getField("gr_carrier_5_rank")
var car6 = this.getField("gr_carrier_6_rank")
var car7 = this.getField("gr_carrier_7_rank")
var car8 = this.getField("gr_carrier_8_rank")
var car9 = this.getField("gr_carrier_9_rank")
var car10 = this.getField("gr_carrier_10_rank")
var car11 = this.getField("gr_carrier_11_rank")
var car12 = this.getField("gr_carrier_12_rank")
var car13 = this.getField("gr_carrier_13_rank")
var car14 = this.getField("gr_carrier_14_rank")
var car15 = this.getField("gr_carrier_15_rank")
var car16 = this.getField("gr_carrier_16_rank")
var car17 = this.getField("gr_carrier_17_rank")
var car18 = this.getField("gr_carrier_18_rank")
var car19 = this.getField("gr_carrier_19_rank")
var t = this.getField("t_js_carrier_ranking") // This is a readonly, hidden field

if((car1.value == "") && (car2.value == "") && (car3.value == "") && (car4.value == "") && (car5.value == "") && (car6.value == "") && (car7.value == "") && (car8.value == "") && (car9.value == "") && (car10.value == "") && (car11.value == "") && (car12.value == "") && (car13.value == "") && (car14.value == "") && (car15.value == "") && (car16.value == "") && (car17.value == "") && (car18.value == "") && (car19.value == ""))

Then – and here is when it turns to ugly brute force -- I was thinking I’d have to cover every possible combination of primary and secondary carriers in order to allow the user to proceed.  Isn’t that over 300 possible combinations? 
I am hoping there's a more streamlined way of accomplishing this!

if((car1.value != "") && (car2.value != ""){

if((car1.value != "") && (car3.value != ""){

if((car1.value != "") && (car4.value != ""){

…etc, another 321 times.

Could someone with a higher skillset please recommend a more efficient means to require only two (yet any two) out of nineteen text fields? 


Posted By: gkaiseril
Date Posted: 13 Oct 2015 at 5:58pm
I would look at using Hierarchical field naming. This type of field naming has a "parent" field with a name like "gr_carrier_rank" and then children fields like "gr_carrier_rank.1", ""gr_carrier_rank.2", and so on. This construction of field is created by creating the children fields. One can then manipulate the common properties in mass by using the parent field. One can also create an array of field objects, the children fields, by using the "getArray()" method.  One can then use the "for" control statement to loop through the children fields and for example sum or count the fields with a given property.

This would allow one to change all the children fields' stroke color to red with one statement.

this.getField(""gr_carrier_rank".strokeColor =;

As for counting the number of checked fields why not use a document variable to increment or decrement when a field is selected one could then issue a warning if more than 2 fields are selected.

Another option would to be to change the stroke color as items are checked.

At the end, I would check to see if only 2 items have been checked and if so change all the stroke colors to black and then use a for loop to find the 2 checked items and set them to a stroke color of red.


Posted By: morrison
Date Posted: 13 Oct 2015 at 6:22pm
Being a novice (zero education in this area, all brute-force and ignorance), I have no idea what a "getArray()" method or "for loop" is, but you provided what look like excellent jumping-off points for research.  Thank you for aiming me in what are surely better directions!!  Smile

Posted By: morrison
Date Posted: 13 Oct 2015 at 6:27pm
Shoot.  As far as Hierarchical field naming goes, the naming conventions by which I am bound do not allow for the dot.  I am limited to letters, numbers, and the underscore.
Will look into the array and "for loop", though!  :-)

Posted By: morrison
Date Posted: 15 Oct 2015 at 6:56pm

   Thank you for your assistance.  The functionality is working, as far as requiring the set of text fields, or flagging them all as optional.  Smile  There are, however, a couple of tweaks I'd like to make but don't know how.  Would you mind telling me if these things can be done and, if so, could you point me in the right direction?

Here is what I have in place to force entry into two of nineteen text fields:

/*********** belongs to: Document-Level:CountCarriers ***********/

function CountCarriers(aNames)
var fieldValue;
var nCount = 0;
for (i = 0; i < aNames.length; i++)
 oField = this.getField(aNames);
 if(oField.value != "") nCount++;
 return nCount;

/*********** belongs to: AcroForm:t_js_carriers_req:Calculate ***********/
// I initially tried a slight variation on this as a validation in each of the text fields.

var aCarriers = new Array("carrier1","carrier2","carrier3","carrier4","carrier5","carrier6","carrier7","carrier8","carrier9","

getField("t_js_carrier_count").value = CountCarriers(aCarriers);

if(this.getField("t_js_carrier_count").value < 2)
 aCarriers.strokeColor =;  // I must be doing something wrong here.

if(this.getField("t_js_carrier_count").value == 2)
 aCarriers.strokeColor =; 
// and here

if(this.getField("t_js_carrier_count").value > 2)
 app.alert("You may assign only one carrier as primary and only one carrier as secondary.",0);
 aCarriers.strokeColor =;
  // and here as well

As you can see, I was trying to set the stroke color to red on the text fields as a group when they're required and set them to black as a group when optional.  Is there a way to set the stroke color in one shot for all fields in an array?

The other problem I was having was when the total exceeded 2. 
If a person enters a value in a third field (or 4th, etc), I would like to wipe out that value... or not even commit it in the first place.  (This occurred when using a validation in each of the text fields.)  Tried adding event.rc = false, as follows:
if(this.getField("t_js_carrier_count").value > 2)
 app.alert("You may assign only one carrier as primary and only one carrier as secondary.",0);
event.rc = false;
 aCarriers.strokeColor =;

but the entry in the violating field(s) still committed, and I would get the alert message several times.  Tried to set the field value(s) to "" as follows:
if(this.getField("t_js_carrier_count").value > 2)
 app.alert("You may assign only one carrier as primary and only one carrier as secondary.",0);
 this.getField("carrier3").value = "";
 aCarriers.strokeColor =;
 but the value(s) still remained in the field(s).

So, in a nutshell, the goals are:
-- Once two of the text fields have data in them, the text fields' stroke colors are black, and the fields are optional.
-- If fewer than two of the text fields contain data, the text fields' stroke colors should be red, and the fields set to required. 
(We have an issue where white space from the database is being recognized by Acrobat; so what I do is set a dummy tag [hidden, readonly] to required or optional.)
-- If the user tries to make an entry in any of the text fields after he has already indicated one carrier as primary and another as secondary, I would like to remove that entry and throw an error message.  I hesistate to lock down the entire fort at that point because we want to allow the users to change entries if they make an error or change their minds. 
Perhaps I can lock down the remaining fields once there are entries in two.  However, in that case, I would have to allow the user to back out his entry from one or both fields, and -- once he does so -- free up the remaining fields so he can make one or two new entries, as appropriate.

Are these goals achievable?

Thank you,

Posted By: morrison
Date Posted: 15 Oct 2015 at 6:57pm
Sorry about the italics!  Didn't meant to apply that formatting!

Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.10 -
Copyright ©2001-2017 Web Wiz Ltd. -