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

Replace option in a concatonation script

Explorer ,
Aug 18, 2021 Aug 18, 2021

Copy link to clipboard

Copied

I was unable to reply to a previous very helpful post. The below script was offered by Thom Parker which combines the elements of 3 fields (plus some other values). The first element takes a 10 character field and adds two dashes after the first character which is always a letter (Format 1 below). It later dawned on me that I need to accomodate adding one dash when a different format is used (Format 2 below). I would be grateful to receive a solution to add the one dash variable to the script. The script pasted below accomplishes "Format 1" plus other items.

 

Format 1: T--123456789 (one letter, two dashes, nine numbers)

Format 2: TE-123456789  (two letters, one dash, nine numbers)

 

In laymens terms I need to add the "two letter, one dash" option. Could we use an if (else?) type code i.e.:

  • "If there's one letter in character 1 then insert two dashes before character 2"
  • "If there's two letters in characters 1 & 2 then insert one dash before character 3"

 

Current script:

event.value = (this.getField("FILE NUMBER").value.replace(/^(\w)/,"$1--") + " Initial #" + this.getField("OFFICER ID").value + " " + this.getField("REPORTING OFFICER").value).toUpperCase();

 

Your assistance is much appreciated. Thank you!

TOPICS
JavaScript , PDF forms

Views

1.3K

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 , Aug 21, 2021 Aug 21, 2021

You can do it using this code as the custom calculation script of the second field:

 

 

var v1 = this.getField("Field 1").valueAsString;
if (v1.length==10) {
	event.value = v1.substring(0,1) + "--" + v1.substring(1, 10);
} else if (v1.length==11) {
	event.value = v1.substring(0,2) + "-" + v1.substring(2, 11);
} else event.value = "";

 

Votes

Translate

Translate
Community Expert ,
Aug 19, 2021 Aug 19, 2021

Copy link to clipboard

Copied

.replace(/^(\w)(?=-)/,"$1--").replace(/^(\w{2})/,"$1--")

Concantenating to replaces should to the trick.

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
Explorer ,
Aug 19, 2021 Aug 19, 2021

Copy link to clipboard

Copied

Thank you!!!

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
Explorer ,
Aug 19, 2021 Aug 19, 2021

Copy link to clipboard

Copied

My apologies. I thought it was working but I was wrong. The incorrect result I am getting when modifying the script is the following;

 

Format 1 when there is one letter in character 1:

  • Current incorrect result: T1-23456789 
  • I'm looking for a result of T--123456789 (one letter, two dashes, nine numbers)

Format 2 when there are two letters in characters 1 & 2:

  • Current correct result: TE-123456789 
  • This is working properly (two letters, one dash, nine numbers)

 

Perhaps I've altered the code incorrectly? Here is what I have that is rendering the above:

event.value = (this.getField("FILE NUMBER").value.replace(/^(\w)(?=-)/,"$1--").replace(/^(\w{2})/,"$1--") + " " + this.getField("Report type label").value + " #" + this.getField("OFFICER ID").value + " " + this.getField("REPORTING OFFICER").value).toUpperCase();

 

Thank you!

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 ,
Aug 19, 2021 Aug 19, 2021

Copy link to clipboard

Copied

try this one:

 

.replace(/^([A-Z]{1,2}-?)/,"$1--") 

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
Explorer ,
Aug 19, 2021 Aug 19, 2021

Copy link to clipboard

Copied

That appears to work great. 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
Explorer ,
Aug 21, 2021 Aug 21, 2021

Copy link to clipboard

Copied

Oh gosh. Now I really look dumb. So I thought it was working but it isn't. I'm thankful for the previous suggestions. Recap of my scenario:

 

Field 1 entered

A999999999

or

AA999999999

 

Field 2 need it to render

A--999999999 (two dashes when there's one letter in character 1)

or

AA-999999999 (1 dash when there's 2 letters in characters 1 and 2)

 

When I use the below much appreciated suggestions:

event.value = this.getField("Field 1").value.replace(/^(\w)(?=-)/,"$1--").replace(/^(\w{2})/,"$1--")

Result:

A9-99999999

AA--999999999

 

event.value = this.getField("Field 1").value.replace(/^([A-Z]{1,2}-?)/,"$1--") 

Result:

A--99999999 YAY CORRECT!

AA--999999999 needs only one dash instead of two

 

Thank you so much for your continued 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 ,
Aug 21, 2021 Aug 21, 2021

Copy link to clipboard

Copied

It's pretty important to layout the exact conditions and result you need up front, in explicit terms.  Thurough testing is also pretty important. 

 

You have outlined two separate conditions that create different results, and since you can't write a conditional into a regular expression, you need two separate regular expressions that test for the different conditions. 

Basically the code that Radzmar provided, but with the pattern change I provided for matching capital letters only. 

You have all the information you need to make the change. Here's an article on using Regular expressions:

https://www.pdfscripting.com/public/Pattern-Matching-with-Regular-Expressions.cfm

  

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
Explorer ,
Aug 21, 2021 Aug 21, 2021

Copy link to clipboard

Copied

Thank you for your patience Thom. I have to be honest that my brain can't comprehend the info on the link you provided but I do appreciate it. Would you be willing to suggest how I can do two seperate regular conditions or  how about if I tried a different approach that doesn't consider letters or numbers but merely number of characters?:

 

Field 1:

OOOOOOOOOO (ten character field)

OOOOOOOOOOO (eleven character field)

 

Field 2 to render:

If field one has 10 characters then add 2 dashes after character 1

If field one has 11 characters then add 1 dash after character 2

 

Is this possible? 

Thank you for your patience.

 

 

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 ,
Aug 21, 2021 Aug 21, 2021

Copy link to clipboard

Copied

You can do it using this code as the custom calculation script of the second field:

 

 

var v1 = this.getField("Field 1").valueAsString;
if (v1.length==10) {
	event.value = v1.substring(0,1) + "--" + v1.substring(1, 10);
} else if (v1.length==11) {
	event.value = v1.substring(0,2) + "-" + v1.substring(2, 11);
} else event.value = "";

 

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 ,
Aug 21, 2021 Aug 21, 2021

Copy link to clipboard

Copied

I edited the code above. Didn't notice you wanted the dash after the second character in the second scenario...

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
Explorer ,
Aug 21, 2021 Aug 21, 2021

Copy link to clipboard

Copied

Soooo close. I'm grateful for the reply. The result I'm getting is:

 

10 characters:

O--OOOOOOOOO

 

11 characters:

O-OOOOOOOOOO (need to scoot the dash one character to the right, but one dash is perfect)

 

Any thoughts? 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
Explorer ,
Aug 21, 2021 Aug 21, 2021

Copy link to clipboard

Copied

I think I got it. Thank you!!!! Whew.

var v1 = this.getField("FILE NUMBER").valueAsString;
if (v1.length==10) {
event.value = v1.substring(0,1) + "--" + v1.substring(1, 10);
} else if (v1.length==11) {
event.value = v1.substring(0,2) + "-" + v1.substring(2, 11);
} else event.value = "";

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 ,
Aug 21, 2021 Aug 21, 2021

Copy link to clipboard

Copied

++ Adding to the discussion,

 

This also worked pretty good for me:

 

 

var f = event.target;
var g = f.value;
var dash1 = "-";
var dash2 = "--";
var m = g.substring(1);

if (g.length == 10) {

  this.getField("Field2").value = g.charAt(0)+ dash2 + m;

} else {

if (g.length == 11) {

  this.getField("Field2").value = g.charAt(0)+ dash1 + m;

} else {

this.getField("Field2").value = f.value;

  }

}

 

In my case, I ran the custom calculation script from the event target field; Field1.

 

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
Explorer ,
Aug 23, 2021 Aug 23, 2021

Copy link to clipboard

Copied

LATEST

Thank you for that. My project is on going so I may reference this and I appreciate it.

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