PowerApps Masking Controls

PowerApps: Masking First 'X' Characters

Premise

In PowerApps we can mask/hide the entire text of a textbox control by changing its Mode to Password. However, in this post I’ll demonstrate how to mask the first ‘X’ characters and only display the last 4 characters, like Credit Card or, Bank Account numbers.

Solution

Following is the formula to mask the first ‘X’ characters of a control except the last 4 digit, in PowerApps.

Concatenate(Concat(ForAll(Split(Left(Parent.Default, (Len(Parent.Default) - 4)),""), "X"), Value), Right(Parent.Default, 4))

The above code snippet can be described as the following:

  • I’m using this control inside a PowerApps form. Hence, my source value is coming from, Parent.Default.
  • First we extracted all the characters from the original value, Parent.Default, excluding the last 4 digit, using the PowerApps Left function.
    Left(Parent.Default, (Len(Parent.Default) - 4))
  • Then I’m passing the extracted text to the Split function.
    Split(Left(Parent.Default, (Len(Parent.Default) - 4)),"")
  • Split will convert the string to array/collection of each character.
  • Then I’m passing this collection to the ForAll function.
  • ForAll will loop over each character of the extracted string and replace the same with the character, ‘X’. We can also use any character here. For ex, ‘*’, ‘#’, etc.
    ForAll(Split(Left(Parent.Default, (Len(Parent.Default) - 4)),""), "X")
  • Once, I’ve replaced all the values of my collection with the character ‘X’, I’ll join this collection as a single string using the Concat function.
    Concat(ForAll(Split(Left(Parent.Default, (Len(Parent.Default) - 4)),""), "X"), Value)
  • Now that I have successfully masked the first ‘X’ characters, I also need to extract the last 4 characters as is i.e., without masking. This can be easily achieved using the Right function.
    Right(Parent.Default, 4)
  • Finally, the 2 strings, masked and normal, can be joined using the Concatenate function.
Concatenate(
    Concat(
        ForAll(
            Split(
                Left(Parent.Default, (Len(Parent.Default) - 4)),""),
             "X"), 
        Value), 
    Right(Parent.Default, 4))
PowerApps: Masked Label

Key Takeaway

  • As I’m using my label control inside a PowerApps forms, the source value is coming from, Parent.Default. If you have any other source then, do replace this with your actual source.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.