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

RegExp to find IP Address and mask in an Adobe Form

Community Beginner ,
Sep 19, 2018 Sep 19, 2018

Copy link to clipboard

Copied

I have an Adobe Acrobat DC form that looks through fields and sees if the user entered an IP address.  If they did it throws an alert and tells the user where to find it.  I'm having two problems:

     1. When I run the function it seems to only work every other time.

     2. I am attempting to use the replace function to mask the IP address but it is not working.  It appears that the usrText in usrText.replace, line 29 returns, 'Undefined'.

function IPAddress(lct)

{

var ipRegExp = /\b(?:(?:2(?:[0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9])\.){3}(?:(?:2([0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9]))\b/ig;  //RegExp to find IP Address's

var usrInput = this.getField(lct).value;  //Get name of field set to run function

var setClass = this.getField("Classification"); //Get current classification

var arrayText = usrInput.split(" ");  //Split the text into an array to find the IP Address

var findIp = usrInput.replace(ipRegExp, "X|X|X|X|");  //Replace IP with unique string to find later

var arrayFindIp = findIp.split(" ");   //Split with the unique IP field to parse through

//For loop to go through each element in the split User Input to locate the IP address position

for (var i=0; i<arrayText.length; i++){

if(arrayFindIp === "X|X|X|X|"){

var IP = arrayText;  //Display the IP address entered by the user

var IPSplit = IP.split(".");  //Split the IP address

var IpMask = "xxx.xxx." + IPSplit[2] + "." + IPSplit[3];  //Mask the IP Address

break;

}

}

//If classification is not secret then alert the user and mask the IP address

if (setClass.value !== "SECRET"){

if(myRegExp.test(usrInput)){

app.alert("IP Address (" + IP + ") found in the " + lct + " field.\n" +

   "The IP Address entered will now be masked.  To unmask the IP, \n" +

   "change the classification to Secret and update the IP Address")

usrInput.replace(IP, IpMask);  //NOT WORKING ---- usrInput is showing undefined

}

} else {

app.alert("IP Address (" + IP + ") found in the " + lct + " field.", 3)  //If the classification is secret then alert the user of the presence of an IP address.

}

}

IPAddress("Description")

//DEBUG

this.getField("Description").value = "This is a 8.8.8.8 test"

//this.getField("Description").value = "This is a test"

Is there anyone that can help me with this?

TOPICS
Acrobat SDK and JavaScript , Windows

Views

558

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

correct answers 1 Correct answer

Community Expert , Sep 19, 2018 Sep 19, 2018

The replace command doesn't actually change the string, and even if it did that won't re-apply the string as the field's value.
So change that line:

usrInput.replace(IP, IpMask);

To:

this.getField(lct).value = usrInput.replace(IP, IpMask);

Votes

Translate

Translate
Community Expert ,
Sep 19, 2018 Sep 19, 2018

Copy link to clipboard

Copied

The replace command doesn't actually change the string, and even if it did that won't re-apply the string as the field's value.
So change that line:

usrInput.replace(IP, IpMask);

To:

this.getField(lct).value = usrInput.replace(IP, IpMask);

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
Community Beginner ,
Sep 19, 2018 Sep 19, 2018

Copy link to clipboard

Copied

That was so simple, I just smacked my forehead and said, "duh".  Thank you for the help.

Second, any reason it would only work every other time? For example I have it set to the field as an on blur command. I have to exit the field twice to get it to run.  Or when I run it in the console I have to run it twice to get it to go also?

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
Community Beginner ,
Sep 19, 2018 Sep 19, 2018

Copy link to clipboard

Copied

That was a typo I replaced them all with IpRegExp.  Your a genius, thanks for your help.

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
Community Expert ,
Sep 19, 2018 Sep 19, 2018

Copy link to clipboard

Copied

LATEST

Yes, it's because you're accessing the value incorrectly.

To get the new value in a custom validation script, for example, you need to use event.value, not this.getField("FieldName").value. That will return the field's old (ie, current) value, not its new one.

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
LEGEND ,
Sep 19, 2018 Sep 19, 2018

Copy link to clipboard

Copied

In line 25 you use myRegExp, but I don't see it defined anywhere.

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