How to check for and eliminate duplicate fields (Acrobat Full v6)

Hi, I am trying to use javascript, which I know very little about, to check a series of 8 text fields to eliminate any duplicates within those fields.  For example, if field1 and field2 are the same, I want to clear field2, then check field1 against field3, etc. with the end result being all unique fields (or empty for the ones that had been duplicates). Can anyone assist?
cafulfordAsked:
Who is Participating?
 
Michel PlungjanConnect With a Mentor IT ExpertCommented:
perhaps there is another i somewhere?

try


var allFields = "";
for(var xx=1;x<=8;xx++) {
  var fld = this.getField("pcname"+xx);
  var val = fld.value;
  if (val =="") continue; // I hope Acrobat supports this
  if (allFields.indexOf("@"+val+"@")!=-1)  { // did we have this value before?
    fld.value="";  // yes, clear the field
  }
  else {
    allFields+="@"+val+"@"; // no, add it to the test string
  }
}


then if you still need the loop


var allFields = "";

for(var i=1;i<=8;i++) {
  for(var xx=1;x<=8;xx++) {
    var fld = this.getField("pcname"+xx);
    var val = fld.value;
    if (val =="") continue; // I hope Acrobat supports this
    if (allFields.indexOf("@"+val+"@")!=-1)  { // did we have this value before?
      fld.value="";  // yes, clear the field
    }
    else {
      allFields+="@"+val+"@"; // no, add it to the test string
    }
  }
}

but it really does not make any sense
0
 
Michel PlungjanIT ExpertCommented:
0
 
cafulfordAuthor Commented:
I am familiarar with that js is and I have a book on it as well but I cat't seem to get it to work.  For example, this is one of the combinations that I have tried with no luck:
{
  if (this.getField("pcname2").value == pcname1) or
  if (this.getField("pcname3").value == pcname1) or
  if (this.getField("pcname4").value == pcname1) or
  if (this.getField("pname1").value  == pcname1) or
  if (this.getField("pname2").value  == pcname1) or
  if (this.getField("pname3").value  == pcname1) or
  if (this.getField("pname4").value  == pcname1) then
      this.getField("pcname1").value = "";
}
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Michel PlungjanIT ExpertCommented:
0
 
Michel PlungjanIT ExpertCommented:
0
 
cafulfordAuthor Commented:
Could you assist with the code example?  I am not a programmer and I have spent so much time at this point that I really am looking for some examples that I can tweak to my needs.  Thanks...
0
 
Michel PlungjanIT ExpertCommented:
Try this (I have never done any acrobat JS)

It is assuming that the field values do not have @ in then. If they might, change @ to for example |

var allFields = "";
for(var i=1;i<=8;i++) {
  var fld = this.getField("pcname"+i);
  var val = fld.value;
  if (allFields.indexOf("@"+val+"@")!=-1)  { // did we have this value before?
    fld.value="";  // yes, clear the field
  }
  else allFields+="@"+val+"@"; // no, add it to the test string
}
0
 
cafulfordAuthor Commented:
That gets me sooooo close.  It works but what is happening is say I have more than 2 of the fields with the same value, i have to run the js more than 1 time to keep checking for the duplicates.  
For example:
pcname1, pcname2 and pcname3 all contain the same value. If I run the script, it will eliminate 1 of the dups, but the other 2 are still dups.  
another example:
pcname1 and pcname2 contain the same value and pcname3 and pcname4 contain the same value but different from pcname 1 and pcname2.

Is there a way to loop it somehow until there are no dups at all?  My intention is to automate this as you exit the Acrobat form.
0
 
Michel PlungjanIT ExpertCommented:
Hmm... I am not sure I get this.

the script works as follows.

Field1 has xxx
Field2 has yyy
Field 3 has xxx
Field 4 has xxx

first it gets field 1 - xxx
that value is not in allFields so it adds
@xxx@ to the allFields string
then it gets field 2 - yyy
that value is not in allFields so it adds
@yyy@ to the allFields string making it @xxx@@yyy@
then it gets field3 - xxx
it looks to see if
@xxx@ is in @xxx@@yyy@ which it is so it blanks field 3
then it gets field 4 - xxx
it looks to see if
@xxx@ is in @xxx@@yyy@ which it is so it blanks field 4

Unless I have overlooked something or you forgot the += in the last statement, then the script should work for any amount of dupes
0
 
cafulfordAuthor Commented:
Yes, I see what you are saying.  I copied and pasted the code and verified that it is exactly what you sent me but it is not doing it as you just stated.  Using your example of:
Field1 has xxx
Field2 has yyy
Field3 has xxx
Field3 has xxx

I need to actually run the script twice to remove the 2 extra fields with xxx in them.  Each time I run the script, it removes 1 of the xxx fields.
0
 
Michel PlungjanIT ExpertCommented:
That sounds like a problem with the javascript engine or perhaps a paste error?

Here is the function in action in a real form

<html>
<head>
<script>

function validate() {
  var allFields = "";
  for(var i=1;i<=8;i++) {
    var fld = this.getField("pcname"+i);
    var val = fld.value;
    if (allFields.indexOf("@"+val+"@")!=-1)  { // did we have this value before?
      fld.value="";  // yes, clear the field
    }
    else allFields+="@"+val+"@"; // no, add it to the test string
  }
  return false; // return false to stay on the form to see the result
}
function getField() {
  return this.elements[arguments[0]]
}
function initPage() {
  document.forms[0].getField=getField;
  document.forms[0].onsubmit=validate;
}
window.onload=initPage;
</script>
</head>
<body>
<form>
1<input type="text" name="pcname1" value=""><br>
2<input type="text" name="pcname2" value=""><br>
3<input type="text" name="pcname3" value=""><br>
4<input type="text" name="pcname4" value=""><br>
5<input type="text" name="pcname5" value=""><br>
6<input type="text" name="pcname6" value=""><br>
7<input type="text" name="pcname7" value=""><br>
8<input type="text" name="pcname8" value=""><br>
<input type="submit">
</form>
</body>
</html>
0
 
cafulfordAuthor Commented:
It could also be something in the way Adobe Acrobat handles things...Just out of curiosity, how would I loop your exact code 8 times?
0
 
Michel PlungjanIT ExpertCommented:
Try for fun


var allFields = "";
for(var i=1;i<=8;i++) {
  var fld = this.getField("pcname"+i);
  var val = fld.value;
  if (allFields.indexOf("@"+val+"@")!=-1)  { // did we have this value before?
    app.alert("clearing field #"+i)
    fld.value="";  // yes, clear the field
    app.alert("cleared field #"+i+"? :"+fld.value)
  }
  else {
    allFields+="@"+val+"@"; // no, add it to the test string
    app.alert("allFields: ["+ allFields+"]");
  }
}


0
 
cafulfordAuthor Commented:
Hi, I ran 2 scenarios: in the 1st the contents of the fields were (only used 4):
pcname1 = bbogdan
pcname2 = bbogdan
pcname3 = sworkman
pcname4 = sworkman

what was returned was:
allFields:[@bbogdan@]      
clearing field #2                  
cleared fields #9            

At that point, pcname2 was cleared but all 3 of the other fields remained the same.  I did it again and I got:
allFields:[@bbogdan@]            
allFields:[@bbogdan@@@]      
allFields:[@bbogdan@@@@sworkman@]      
clearing field #4                        
cleared field #9
_____________________________________________________
In the 2nd scenario the contents of the fields were (only used 4):
pcname1 = bbogdan
pcname2 = bbogdan
pcname3 = bbognan
pcname4 = bbogdan

what was returned was:
allFields:[@bbogdan@]      
clearing field #2                  
cleared field #9              

At that point, pcname2 was cleared but all 3 of the other fields remained the same.  I did it again and I got:
allFields:[@bbogdan@]            
allFields:[@bbogdan@@@]      
clearing field #3                        
cleared field #9

At that point, pcname2 and pcname3 were cleared but pcname4 remained the same.  I did it again and I got:
allFields:[@bbogdan@]          
allFields:[@bbogdan@@@]  
clearing field #3
cleared field #3
clearing field #4
cleared field #9

I reall appreciate all your help...here is the code exactly as I pasted it so you know:

// check for duplicates

var allFields = "";
for(var i=1;i<=8;i++) {
  var fld = this.getField("pcname"+i);
  var val = fld.value;
  if (allFields.indexOf("@"+val+"@")!=-1)  { // did we have this value before?
    app.alert("clearing field #"+i)
    fld.value="";  // yes, clear the field
    app.alert("cleared field #"+i+"? :"+fld.value)
  }
  else {
    allFields+="@"+val+"@"; // no, add it to the test string
    app.alert("allFields: ["+ allFields+"]");
  }
}

0
 
Michel PlungjanIT ExpertCommented:
I am confused

Field 9?
0
 
Michel PlungjanIT ExpertCommented:

this will not give false positives

var allFields = "";
for(var i=1;i<=8;i++) {
  var fld = this.getField("pcname"+i);
  var val = fld.value;
  if (val =="") continue; // I hope Acrobat supports this
  if (allFields.indexOf("@"+val+"@")!=-1)  { // did we have this value before?
    app.alert("clearing field #"+i)
    fld.value="";  // yes, clear the field
    app.alert("cleared field #"+i+"? :"+fld.value)
  }
  else {
    allFields+="@"+val+"@"; // no, add it to the test string
    app.alert("allFields: ["+ allFields+"]");
  }
}
0
 
cafulfordAuthor Commented:
Right, no false positives but field #9 still shows and it has to be run the same # of times to clear all dups for both examples.  Can a loop be added as a "wrapper" around the code so it runs automatically x # of times?
0
 
Michel PlungjanIT ExpertCommented:
Sure, but where does #9 come from???

Michel
0
 
cafulfordAuthor Commented:
I have absolutely no idea...The code is exactly as it should be.  I did not re-type a thing. what would the code be to add the loop?
0
 
cafulfordAuthor Commented:
Michel,

that was it!  (the last one without the loop).  thank you so much.  Really appreciated!

Charlie
0
 
Michel PlungjanIT ExpertCommented:
Extreme... So there was a variable somwhere called "i" that I did not know about ...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.