Planet PDF Forum Planet PDF Forum
  New Posts New Posts RSS Feed - require any 2 of many text fields
  FAQ FAQ  Forum Search   Register Register  Login Login

Hi, welcome to the Foxit Planet PDF Forum. If you have PDF or Adobe Acrobat questions then the right place to ask them is here, in this forum.

require any 2 of many text fields

 Post Reply Post Reply
Author
morrison View Drop Down
New Member
New Member
Avatar

Joined: 26 Jan 2015
Points: 32
Post Options Post Options   Quote morrison Quote  Post ReplyReply Direct Link To This Post Topic: require any 2 of many text fields
    Posted: 13 Oct 2015 at 4:53pm
Hi,

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 == ""))
{
t.required=true;
car1.strokeColor=color.red;
car2.strokeColor=color.red;
car3.strokeColor=color.red;
car4.strokeColor=color.red;
car5.strokeColor=color.red;
car6.strokeColor=color.red;
car7.strokeColor=color.red;
car8.strokeColor=color.red;
car9.strokeColor=color.red;
car10.strokeColor=color.red;
car11.strokeColor=color.red;
car12.strokeColor=color.red;
car13.strokeColor=color.red;
car14.strokeColor=color.red;
car15.strokeColor=color.red;
car16.strokeColor=color.red;
car17.strokeColor=color.red;
car18.strokeColor=color.red;
car19.strokeColor=color.red;
}

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 != ""){
t.required=false;
car3.strokeColor=color.black;
car4.strokeColor=color.black;
car5.strokeColor=color.black;
car6.strokeColor=color.black;
car7.strokeColor=color.black;
car8.strokeColor=color.black;
car9.strokeColor=color.black;
car10.strokeColor=color.black;
car11.strokeColor=color.black;
car12.strokeColor=color.black;
car13.strokeColor=color.black;
car14.strokeColor=color.black;
car15.strokeColor=color.black;
car16.strokeColor=color.black;
car17.strokeColor=color.black;
car18.strokeColor=color.black;
car19.strokeColor=color.black;
}

if((car1.value != "") && (car3.value != ""){
t.required=false;
car2.strokeColor=color.black;
car4.strokeColor=color.black;
car5.strokeColor=color.black;
car6.strokeColor=color.black;
car7.strokeColor=color.black;
car8.strokeColor=color.black;
car9.strokeColor=color.black;
car10.strokeColor=color.black;
car11.strokeColor=color.black;
car12.strokeColor=color.black;
car13.strokeColor=color.black;
car14.strokeColor=color.black;
car15.strokeColor=color.black;
car16.strokeColor=color.black;
car17.strokeColor=color.black;
car18.strokeColor=color.black;
car19.strokeColor=color.black;
}

if((car1.value != "") && (car4.value != ""){
t.required=false;
car2.strokeColor=color.black;
car3.strokeColor=color.black;
car5.strokeColor=color.black;
car6.strokeColor=color.black;
car7.strokeColor=color.black;
car8.strokeColor=color.black;
car9.strokeColor=color.black;
car10.strokeColor=color.black;
car11.strokeColor=color.black;
car12.strokeColor=color.black;
car13.strokeColor=color.black;
car14.strokeColor=color.black;
car15.strokeColor=color.black;
car16.strokeColor=color.black;
car17.strokeColor=color.black;
car18.strokeColor=color.black;
car19.strokeColor=color.black;
}

…etc, another 321 times.
Wacko

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

Regards,
morrison

Back to Top
gkaiseril View Drop Down
Senior Member
Senior Member


Joined: 15 Jul 2011
Location: USA
Points: 4109
Post Options Post Options   Quote gkaiseril Quote  Post ReplyReply Direct Link To This Post 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 = color.red;

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.

 


Edited by gkaiseril - 13 Oct 2015 at 6:00pm
Back to Top
morrison View Drop Down
New Member
New Member
Avatar

Joined: 26 Jan 2015
Points: 32
Post Options Post Options   Quote morrison Quote  Post ReplyReply Direct Link To This Post 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

Back to Top
morrison View Drop Down
New Member
New Member
Avatar

Joined: 26 Jan 2015
Points: 32
Post Options Post Options   Quote morrison Quote  Post ReplyReply Direct Link To This Post 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!  :-)
Back to Top
morrison View Drop Down
New Member
New Member
Avatar

Joined: 26 Jan 2015
Points: 32
Post Options Post Options   Quote morrison Quote  Post ReplyReply Direct Link To This Post Posted: 15 Oct 2015 at 6:56pm
gkaiseril,

   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","
carrier10","carrier11","carrier12","carrier13","carrier14","carrier15","carrier16","carrier17","gcarrier18","carrier19");

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

if(this.getField("t_js_carrier_count").value < 2)
{
 this.getField("t_js_carriers_req").required=true;
 aCarriers.strokeColor = color.red;  // I must be doing something wrong here.
}

if(this.getField("t_js_carrier_count").value == 2)
{
 this.getField("t_js_carriers_req").required=false;
 aCarriers.strokeColor = color.black; 
// 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 = color.red;
  // 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 = color.red;
}

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 = color.red;
}
 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,
~morrison

Back to Top
morrison View Drop Down
New Member
New Member
Avatar

Joined: 26 Jan 2015
Points: 32
Post Options Post Options   Quote morrison Quote  Post ReplyReply Direct Link To This Post Posted: 15 Oct 2015 at 6:57pm
Sorry about the italics!  Didn't meant to apply that formatting!
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

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

This page was generated in 0.047 seconds.