Skip to main content
buddycafe
Known Participant
June 1, 2016
Answered

Display last four of a string of numbers.

  • June 1, 2016
  • 2 replies
  • 5899 views

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

This topic has been closed for replies.
Correct answer try67

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


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

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

2 replies

Inspiring
June 1, 2016

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

buddycafe
buddycafeAuthor
Known Participant
June 1, 2016

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

-Ed

try67
Community Expert
Community Expert
June 1, 2016

Sure. For example:

var fullString = "1234567890";

var lastFour = fullString.substr(-4);

buddycafe
buddycafeAuthor
Known Participant
June 1, 2016

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);
buddycafe
buddycafeAuthor
Known Participant
June 1, 2016

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?


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