Power Apps

Power Apps Phone Number Formatting For Any Country (Input Mask)

Power Apps Phone Number Formatting For Any Country (Input Mask) Posted by - Matthew Devaney on - November 15, 2021 47 Comments Update: March 27, 2022 I’ve wr...

Matthew Devaney
Published
Reading time
5 min read

Before you start

Is this guide for you?

Best entry point
Power Apps
Time investment
5 min read

Imported reference from Matthew Devaney's blog for learning purposes. Original: https://www.matthewdevaney.com/powerapps-phone-number-formatting-for-any-country-input-mask/. Author: Matthew Devaney.

Power Apps Phone Number Formatting For Any Country (Input Mask) Posted by - Matthew Devaney on - November 15, 2021 47 Comments Update: March 27, 2022

I’ve written a new version of this article that shows how to implement phone number input masks inside of a Power Apps form .

Input masks are used to restrict the values input into a text field and apply formatting. Phone numbers and zip codes are an example of a data types that benefit from input masking. Each have specific rules about how they should be formatted. Power Apps does not have a feature to format phone number but we can build an input mask ourselves with this method that I’ve hacked together. In this article I will show you how to apply phone number formatting to a Power Apps text field for any country including the US, Canada, UK, Japan and India. Table of Contents

Introduction: Text Input With Phone Number Formatting Use A Slider To Detect KeyPresses Define The Phone Number Format

Phone Number Format Examples For Other Countries (Optional)

Update The Phone Number Formatting After Each KeyPress

Input A Phone Number With Formatting Applied Introduction: Text Input With Phone Number Formatting

We will create a text field to capture a phone number in the format ###-###-#### . There will also be instructions on how define your own custom format for a different country. The user is restricted to only typing number values. Formatting is automatically applied each time a new number is typed as shown below. Use A Slider To Detect KeyPresses

To show a phone number in the proper format we need to update the text input after each keypress. A text input’s OnChange property cannot detect this because it only triggers once the user moves away from it. However, we can use a slider that will immediately update its value based on the length of the text input value and trigger the OnChange behaviour property inside the slider. This technique makes it act more like an OnKeyPress property than an OnChange property.

Open Power Apps Studio and create a new tablet app from blank. Insert a text input and a label with the words “Phone Number” onto the screen.

Next, place a new slider control directly below the text input. We will use it to count the length of the word in the text input as it is being typed.

Use this code in the Default property of the slider to determine the text input length. Len(txt_PhoneNumber)

When we add numbers to the text input the slider increases and when we remove numbers the slider decreases. Define The Phone Number Format

We will use another text input field to define the phone number format so it can be easily updated for a different locale in the future. Insert a new text input and label onto the screen as shown below.

Copy and paste this code into the OnChange property of txt_PhoneNumberFormat. This code is quite complex, but what we are doing here is creating a collection to define how the phone number should be formatted at each individual number position.

ClearCollect( colPhoneNumberFormat, With( { varTextPhoneNumberFormat : txt_PhoneNumberFormat.Text}, With( {

varTablePhoneNumberFormat : ForAll( Sequence(Len(varTextPhoneNumberFormat)), With( {

varNumberCurrentLength : Left( varTextPhoneNumberFormat, Value ) }, { NumberFormat : varNumberCurrentLength,

CountNumbers : CountRows( MatchAll( varNumberCurrentLength, "#"

) ) } ) ) }, ForAll( Sequence( Max( varTablePhoneNumberFormat, CountNumbers ) ), LookUp( varTablePhoneNumberFormat, CountNumbers = Value ) ) ) ) ) Code language: JavaScript ( javascript )

At this point we can also set a size limit for the phone number text input. Use this code in the MaxLength property of txt_PhoneNumber. Len ( txt_PhoneNumberFormat .Text ) Code language: CSS ( css )

Then type the phone number format “###-###-####” into the Default property of the text input. "## #- ## #- ####" Code language: CSS ( css )

The default phone number format should appear inside the text as shown below.

Phone Number Format Examples For Other Countries (Optional)

We can also choose another phone number format if we desire. Here are some examples for other countries. Copy them into the Phone Number Format text input to see the result. Country Example NumberFormat Canada 204-998-8344 ###-###-#### US (204) 998-8344 (###) ###-#### United Kingdom +44 1234 567890 +## #### ###### Japan (03)-4567-1234 (##)-####-#### India +91 0123 456789 +## ####-######

Update The Phone Number Formatting After Each KeyPress

As the user updates the phone number in the text input we must determine what formatting to apply after each keypress.

Use this code in the OnChange property of the slider. When a new character is added to the text input we use the MatchAll function to extract only the numbers. Then we apply the formatting we generated using our collection. Set ( varPhoneNumber, With( {

varTextNumbersOnly : Concat( MatchAll( txt_PhoneNumber.Text, Match.Digit ), FullMatch ) }, Text( Value(varTextNumbersOnly), LookUp( colPhoneNumberFormat, CountNumbers = Len(varTextNumbersOnly), NumberFormat ) ) ) ); Reset(txt_PhoneNumber); Code language: JavaScript ( javascript )

The text input is reset to the value of the varPhoneNumber variable whenever a formatting change is applied.

Use this code in the Default property of the formula. varPhoneNumber

Input A Phone Number With Formatting Applied

We are done creating the input mask. Test a the text input by typing a phone number and watch it become formatted.

Finally, we should hide the slider, and phone number format label/text input by setting their Visible property to false . false Code language: JavaScript ( javascript ) Did You Enjoy This Article? 😺

Subscribe to get new Copilot Studio articles sent to your inbox each week for FREE Enter your email address Sign Me Up Questions?

If you have any questions or feedback about Power Apps Phone Number Formatting For Any Country (Input Mask) please leave a message in the comments section below. You can post using your email address and are not required to create an account to join the discussion. Data Validation Input Mask Matthew Devaney M365 Copilot Power Apps

How To Create Copilot Custom UI Widgets In Power Apps

Share this

Tagged

Power Apps

Have a Microsoft 365 topic idea?

Share article suggestions, community session ideas, corrections, or real-world scenarios for future nxtM365 learning notes.

Suggest a topic

Keep learning Microsoft 365

Explore more practical guides for SharePoint, Power Platform, Copilot Studio, migration, automation, governance, and security.

Continue learning

Next action

What to do next

Browse all tutorials →