These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Is it possible to search a string in an other textfield?

pablosos
Registered: Apr 11 2011
Posts: 5
Answered

Hello,
 
I want to have a textfield, which's content should be checked, whether an search term, entered in an other textfield, is in it.
 
I'm not so good in scripting, can anybody give me a hint how to realize this function?

My Product Information:
LiveCycle Designer, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The core JavaScript model contains several functions for searching/matching text. The simplest and easiest function to use is the "String.indexOf()" function.

Here's a some code that searches "TextField1" for text entered into "TextFeild2"

var nPos = TextField1.rawValue.indexOf(TextField2.rawValue);
if(nPos >= 0)
app.alert("Text Found");

The indexOf function returns -1 if the text is not found.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The core JavaScript model contains several functions for searching/matching text. The simplest and easiest function to use is the "String.indexOf()" function.

Here's a some code that searches "TextField1" for text entered into "TextFeild2"

var nPos = TextField1.rawValue.indexOf(TextField2.rawValue);
if(nPos >= 0)
app.alert("Text Found");

The indexOf function returns -1 if the text is not found.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

pablosos
Registered: Apr 11 2011
Posts: 5
Thank you, it works. Is it also possible to return a value how often it appears in textfield1?

The nPos var counts a little bit weird for me, so it returns 11 for a search-term which is 3 times in the text.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Accepted Answer
It's finding the first match and returns the index of the start of the found text. There is second optional input to the "indexOf" function, which is the position at which to start the search. So you could write this as a loop, using the index of previous search as the start of the next search.

var nStart = 0, nPos, nCnt=0;
do{
nPos = TextField1.rawValue.indexOf(TextField2.rawValue, nStart);
nStart = nPos+1;
if(nPos >=0) nCnt++;
}while(nPos >= 0)

There are also other ways this could be done. For example, with a regular expression. But to do this effectively you'll need a JavaScript Reference and some skill at programming.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

pablosos
Registered: Apr 11 2011
Posts: 5
Thanks. The nCount was, what I was looking for!