Skip to main content
Participant
September 19, 2018
Answered

RegExp to find IP Address and mask in an Adobe Form

  • September 19, 2018
  • 2 replies
  • 964 views

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?

This topic has been closed for replies.
Correct answer try67

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);

2 replies

Inspiring
September 19, 2018

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

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
September 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);

Participant
September 19, 2018

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?

Participant
September 19, 2018

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