Expand my Community achievements bar.

SOLVED

Correct Method for textfield word search

Avatar

Level 5

I want to write a javascript to search a textfield for a given value say "104B-XX-XXX" the "X"s represent any numbers between 0-9.

If the textfield contains this value (e.g. 104B-38-293) I would like to set a global variable value to equal this 104B-38-293.

I thought a RegExp would be the correct method to use for this situation, however I am having trouble understanding how to apply the RegExp to the entire textfield and obtain the value (104B-38-293).

Would someone please explain how I search the textfield for this value using a RegExp? RegExp now doesn't seem appropriate because I want to copy the value from the textfield.

https://workspaces.acrobat.com/?d=MQ5XegWGYqwF21Rdv8H1Kg

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi,

You can do this with a regexp, using a capturing parenthesis and the exec() method, so;

function fxbsearch(number)

{

    var r = new RegExp("(104B-\\d\\d-\\d\\d\\d)");

    var result = r.exec(number);

    if (result !== null)

    {

        return result[1];

    }

    return "";

}

There are a couple of other problems with your form, you need to use rawValue to refer to the content of a text field (not value), you need to prefix functions in a script object with the script object name, so

BNUMBER.value = fxbsearch.fxbsearch(this.rawValue);

not

BNUMBER.value = fxbsearch(this.rawValue);

And functions don't have a semi-colon after the name.  Have you got "Show console on errors and messages" selected in Acorbat under Edit ... Preferences ... JavaScript.

Here is my version of your form, https://workspaces.acrobat.com/?d=h7Xtbbf8lgx*5QOXob4OqQ

Regards

Bruce

View solution in original post

2 Replies

Avatar

Correct answer by
Level 10

Hi,

You can do this with a regexp, using a capturing parenthesis and the exec() method, so;

function fxbsearch(number)

{

    var r = new RegExp("(104B-\\d\\d-\\d\\d\\d)");

    var result = r.exec(number);

    if (result !== null)

    {

        return result[1];

    }

    return "";

}

There are a couple of other problems with your form, you need to use rawValue to refer to the content of a text field (not value), you need to prefix functions in a script object with the script object name, so

BNUMBER.value = fxbsearch.fxbsearch(this.rawValue);

not

BNUMBER.value = fxbsearch(this.rawValue);

And functions don't have a semi-colon after the name.  Have you got "Show console on errors and messages" selected in Acorbat under Edit ... Preferences ... JavaScript.

Here is my version of your form, https://workspaces.acrobat.com/?d=h7Xtbbf8lgx*5QOXob4OqQ

Regards

Bruce

Avatar

Level 5

Bruce thank you for helping me get around that javascript "barricade". Thank you for the reminders on calling functions from Script Objects. Yes I use the console often.

The reason I used  .value instead of .rawValue was due to certain variables being Global Variables. I beleive you call them differently than a textfield. Thanks again for your help. Much appreciated. I updated my form for others to use as an example.

https://workspaces.acrobat.com/?d=UvVdbT2TbKRr4xexeaVijw