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

Display last four of a string of numbers.

Community Beginner ,
Jun 01, 2016 Jun 01, 2016

Copy link to clipboard

Copied

Hello,

Is there a script to display only the last 4 of a string of numbers where the full number of numerical characters can very?

Either display as "xxxx2298" or "2298". Or any other method.

Thanks,

Ed

TOPICS
Acrobat SDK and JavaScript

Views

4.8K

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 , Jun 01, 2016 Jun 01, 2016

In that case you can use this code as the field's custom calculation script:

event.value = this.getField("MEM_NMBR").valueAsString.substr(-4);

Votes

Translate

Translate
Community Expert ,
Jun 01, 2016 Jun 01, 2016

Copy link to clipboard

Copied

Sure. For example:

var fullString = "1234567890";

var lastFour = fullString.substr(-4);

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 ,
Jun 01, 2016 Jun 01, 2016

Copy link to clipboard

Copied

Thanks for the reply try67,

I inserted this to the "custom calculation script" section but it didn't take. Assuming "1234567890" needs to be changed to my actual mapping (tag)?

Example correct?

  1. var fullString = "MEM_NMBR"
  2. var lastFour = fullString.substr(-4);

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 ,
Jun 01, 2016 Jun 01, 2016

Copy link to clipboard

Copied

The code I provided is generic, to show how it's done.

Are you copying the value from another field? If so, what's the name of that field?

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 ,
Jun 01, 2016 Jun 01, 2016

Copy link to clipboard

Copied

I generally do copy from another (to be hidden) field. Where the hidden field name will be "MEM_NMBR".

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 ,
Jun 01, 2016 Jun 01, 2016

Copy link to clipboard

Copied

In that case you can use this code as the field's custom calculation script:

event.value = this.getField("MEM_NMBR").valueAsString.substr(-4);

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 ,
Jun 01, 2016 Jun 01, 2016

Copy link to clipboard

Copied

This worked. 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 Beginner ,
Jun 13, 2016 Jun 13, 2016

Copy link to clipboard

Copied

Try67, can you please help me flip this around to display the first 17 characters?

event.value = this.getField("MEM_NMBR").valueAsString.substr(-4);

-Ed

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 ,
Jun 13, 2016 Jun 13, 2016

Copy link to clipboard

Copied

event.value = this.getField("MEM_NMBR").valueAsString.substr(0,17);

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 ,
Jun 13, 2016 Jun 13, 2016

Copy link to clipboard

Copied

If I were to write the first question's solution to a more generalized form, I could change the number of last characters to display and compute the number of characters for the fill.

The code for the custom format could look like:

function DispalyLastNChars(nLength, cString)

{

var nRepeat = cString.length - nLength; // numbef of nod-display characters;

var cCover = "*".repeat(nRepeat); // string for non-display characters;

var cDisplay = cString.substr( nLength - nRepeat - 1, nLength); // stirng of characters to display;

return cCover + cDisplay; // displayed and non-displayed characters;

}


if(event.value != "")
{
event.value = DispalyLastNChars(4, event.value);
}


By changing the parameters for the "substr" method to start at the 0 character and go for the specified number of character and changing the order for combining the characters to display and hide, one could have:


function DispalyFirstNChars(nLength, cString)
{
var nRepeat = cString.length - nLength; // numbef of nod-display characters;
var cCover = "*".repeat(nRepeat); // string for non-display characters;
var cDisplay = cString.substr(0, nLength); // stirng of characters to display;
return cDisplay + cCover ; // displayed and non-displayed characters;
}
if(event.value != "")
{
event.value = DispalyFirstNChars(4, event.value);
}

You can read more about MDN JavaScript at JavaScript | MDN . "substr" is a method of the buitin "string" object.

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 ,
Oct 12, 2018 Oct 12, 2018

Copy link to clipboard

Copied

Dear Try67,

How to masking the account number with “*” up to the last 4 digits in adobe forms.

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 ,
Oct 12, 2018 Oct 12, 2018

Copy link to clipboard

Copied

See my post below or above. Here is the code and the location to place the code:

// custom format script;

if(event.value != "" && event.value.length > 4)

{

event.value = "*".repeat(event.value.length - 4) + event.value.substr(event.value.length - 4);

}

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 ,
Nov 23, 2018 Nov 23, 2018

Copy link to clipboard

Copied

LATEST

Hi gkaiseril,

Needed your help! how to execute the below process in formcalc or java script.
below the form created in adobe livecycle designer.
we have added no.of item in table,but the value and description all are cumulative in below table and sum the values.

Capture.JPG

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 ,
Jun 01, 2016 Jun 01, 2016

Copy link to clipboard

Copied

Do you want to display only the last 4 with or without redacted leading positions/

You can have the form retain the full value of a field but only have that field display the last 4 digits. This is not the most secure approach since it is possible to still get to the full value of the field through JavaScript or some of the form tools.

The custom format script could be:

if(event.value != "" && event.value.length > 4)

{

event.value = "*".repeat(event.value.length - 4) + event.value.substr(event.value.length - 4);

}

You may need to create a custom keystroke script to limit the input to numbers only. The following script will limit keystrokes to numbers:

if(event.willCommit == false)
{
event.rc = /^\d*$/.test(event.change);
}

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 ,
Jun 01, 2016 Jun 01, 2016

Copy link to clipboard

Copied

So many ways to skin a cat. Thank you. I made note of your suggestion. Totally makes sense.

-Ed

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