• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to use variables to reduce redundant portions of references in validation script.

New Here ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

I'm trying setup a validation script that confirms that the user has entered a pre-approved number, and returns an app alert if they do not. I have a the script functioning properly but the array of pre-approved numbers is too large for acrobat (text it too large to be displayed in this dialog). I'm trying to use variables for the redundant portions of these numbers to reduce the overall script size.

Here's a sample of my script with a much smaller array of pre-approved numbers.

event.rc = true;

if (event.value != 1476542 && event.value != 1471222 && event.value != 1476895 && event.value != 1471248 && event.value != 1472347 && event.value != 1476246 && event.value != 1476254 && event.value != 1472833

)

{

    app.alert("NOTE: You must enter a pre-approved number.", 3);

    event.rc = false;

}

Notice how each number begins with 147. What I'm wondering is if I can use a variable to replace 147, thereby reducing the size of the script. I would also use variables to replace 148, 149, etc..

I tried this and it did properly recognize the pre-approved number, but it no longer triggered the app alert when a non-approved number was entered.

var x = 147

event.rc = true;

if (event.value != x6542 && event.value != x1222 && event.value != x6895 && event.value != x1248 && event.value != x2347 && event.value != x6246 && event.value != 1476254 && event.value != 1472833

)

{

    app.alert("NOTE: You must enter a pre-approved number.", 3);

    event.rc = false;

}

Thanks for your thoughts!

TOPICS
Acrobat SDK and JavaScript , Windows

Views

328

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

Try this:

event.value != (x * 10000 + 6542) and so on

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 09, 2018 May 09, 2018

Copy link to clipboard

Copied

"If statements" are great for evaluation when there is one or very few creteria to evaluate.  It quickly becomes un-readable when there are many.

I would advise to put all correct entries in an array and then evaluate the occurence of a correct entry by the user.  The indexOf() method of the array object returns the index (0 - based) at which the element you are looking for is placed inside that array.  If the element is not present in the array, it returns -1.

var goodNumbers = [1476542, 1471222, 1476895, 1471248]

if (goodNumbers.indexOf(event.value) == -1)  //this is the equivalent as "Not in the array"

if all correct entries start of by 147, you should not have to write it down yourself.  Have JS do it for you.  You can loop throught an array and add the string "147" to every element:

var goodNumbers = [6542, 1222, 6895, 1248]

for (i in goodNumbers) goodNumbers = "147"+goodNumbers

if (goodNumbers.indexOf(event.value) == -1)

And finally, if you need to sort them out, the sort() method will take care of business.  Just output the array in the console or inside a field, and then copy/past it mannually back to your script.  Not only can you use JS to add functionnality to your form, but you can also build tools for you to use as a developper, lol.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 09, 2018 May 09, 2018

Copy link to clipboard

Copied

Thanks! Trying this now but still having an issue. Even when an approved number is entered, it is returning a value that triggers the non-approved app alert.

Starting with a small array until it functions properly.

var goodNumbers = [1476542, 1471222, 1476895, 1471248]

if (goodNumbers.indexOf(event.value) == -1)

{

    app.alert("NOTE: You must enter a pre-approved number.", 3);

}

else

{

app.alert("Approved.", 3);

}

What am I missing? Also, this array is 10,685 7 digit numbers. Is this even feasible?

Thanks again!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 09, 2018 May 09, 2018

Copy link to clipboard

Copied

The elements in the array have a data type which is Number

The value entered by the user is of data type String

The evaluation considers also the data type so what you should do is force the value entered by the user to change data type like so:

var goodNumbers = [1476542, 1471222, 1476895, 1471248]

if (goodNumbers.indexOf(Number(event.value)) == -1)

{

    app.alert("NOTE: You must enter a pre-approved number.", 3);

}

else

{

app.alert("Approved.", 3);

}

Now it doesn't mather how much elements are in the array but a large number could slow the evaluation.  Please promise me you will not type 10 000+ numbers manually, lol.  Use JS and loops to populate your array.  The push() method will add an element at the end of the array.  If it slows to the point it is not usable, consider using multiple arrays like one for odd numbers, one for even numbers, etc......

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 09, 2018 May 09, 2018

Copy link to clipboard

Copied

Thanks!

I will definitely not be typing them out! I use a formula in excel to create a column of number + ", " and then paste that into word. Then I run a find and replace for the paragraph mark and presto, one huge block of numbers separated by commas.

To use multiple arrays, would validation then just be a matter of using if, if else statements for each array?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 09, 2018 May 09, 2018

Copy link to clipboard

Copied

LATEST

Exactly, you would nest a couple if statement (Good usage limits 3 level deep nesting).

to test if a number is odd or even use the modulo operator:

(myValue % 2 == 0) //even

(myValue % 2 != 0) //odd

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines