RegExp to find IP Address and mask in an Adobe Form

Community Beginner ,
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?

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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

TOPICS
Acrobat SDK and JavaScript, Windows

Views

154

Likes

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

RegExp to find IP Address and mask in an Adobe Form

Community Beginner ,
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?

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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

TOPICS
Acrobat SDK and JavaScript, Windows

Views

155

Likes

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
Sep 19, 2018 0
Most Valuable Participant ,
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);

Likes

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
Reply
Loading...
Sep 19, 2018 2
Community Beginner ,
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?

Likes

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
Reply
Loading...
Sep 19, 2018 0
Community Beginner ,
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.

Likes

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
Reply
Loading...
Sep 19, 2018 0
try67 LATEST
Most Valuable Participant ,
Sep 19, 2018

Copy link to clipboard

Copied

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.

Likes

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
Reply
Loading...
Sep 19, 2018 2
Adobe Community Professional ,
Sep 19, 2018

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Sep 19, 2018 2