Over a million developers have joined DZone.

Nicely Format Phone Numbers in WP7

DZone's Guide to

Nicely Format Phone Numbers in WP7

· Mobile Zone
Free Resource

Launching an app doesn’t need to be daunting. Whether you’re just getting started or need a refresher on mobile app testing best practices, this guide is your resource! Brought to you in partnership with Perfecto

This is a simple requirement: the user types in some string that should be a phone number. You need to format that string to look nice and neat. Assuming that we are in the United States where there are 10 digit numbers and a country code of +1, here’s the solution:


In the code above we remove everything that is not a number. Then we strip the country code. Then we have to handle different lengths. Only lengths of 7 and 10 are truly valid. Greater than 10, we assume it is an extension and we format it as such.

Get the code here.

The output should look something like this:


I hope this helps you while you are developing. Best of luck!

Source:  http://blog.jerrynixon.com/2011/12/format-phone-numbers.html

Keep up with the latest DevTest Jargon with the latest Mobile DevTest Dictionary. Brought to you in partnership with Perfecto.


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}