Home
    Shop
    Advertise
    Write For Us
    Affiliate
    Newsletter
    Contact

Function to Format String As Phone Number

Very often we have to gather various information about application users, including customer's phone numbers. The problem is that user could insert same phone number in many different formats, like in examples bellow:

1234567890
123 456 7890
(123) 456-7890
123 - 456 - 7890
and so on...

 

To keep data consistent and also have it look more professional, it's good to convert all users' different phone number formats to one standard format. Function bellow converts random phone number format to given standard format. Function has two parameters. First parameter is input phone number. Second parameter is desired format. If second parameter is empty string, then it uses default standard US phone format (###) ###-####. Here is the format phone number function:

[ C# ]

using System.Text.RegularExpressions;


public static string formatPhoneNumber(string phoneNum, string phoneFormat)
{

    if (phoneFormat == "")
    {
    // If phone format is empty, code will use default format (###) ###-####
    phoneFormat = "(###) ###-####";
    }

    // First, remove everything except of numbers
    Regex regexObj = new Regex(@"[^\d]");
    phoneNum = regexObj.Replace(phoneNum, "");

    // Second, format numbers to phone string
    if (phoneNum.Length > 0)
    {
    phoneNum = Convert.ToInt64(phoneNum).ToString(phoneFormat);
    }

    return phoneNum;
}

[ VB.NET ]

Public Shared Function formatPhoneNumber(phoneNum As String, phoneFormat As String) As String

    If phoneFormat = "" Then
        ' Default format is (###) ###-####
        phoneFormat = "(###) ###-####"
    End If

    ' First, remove everything except of numbers
    Dim regexObj As Regex = New Regex("[^\d]")
    phoneNum = regexObj.Replace(phoneNum, "")

    ' Second, format numbers to phone string
    If phoneNum.Length > 0 Then
        phoneNum = Convert.ToInt64(phoneNum).ToString(phoneFormat)
    End If

    Return phoneNum
End Function

Phone Format Examples

Different countries have different formats for phone numbers. By default, if second parameter is empty string, format phone number function uses standard US and Canada phone format (###) ###-####, but format can be whatever you want.

Here are some common phone number formats by country:

CountryPhone format
Denmark:## ## ## ##
France:## ## ## ## ##
Germany:##### ######
Iceland:###-#####
Netherlands:###-######
Norway:## ## ## ##
Poland:###-###-###
Switzerland:### ### ## ##
Turkey:#### ### ## ##
Pakistan:### ### ###
India:####-#######
India (mobile):#####-#####
China:(####) ####-####
China (mobile):### #### ####
Hong Kong:#### ####
Japan:(###) ####-####
Australia:## #### ####
Australia (mobile):#### ### ###

Format Phone Number function remarks

This function expects phone number with certain number of digits, as defined in format string. If phone number has less digits than required, format string will be filled from right side and left side will be missing. Let say, user inserted incomplete phone number 12345. By using default format, function will return formatted string filled from right side.

On same way, if phone number is longer then expected, excessive digits will be added on left side.

One more possible issue could be if phone number contains country code. Country codes could be of different length. USA and Canada code is simply +1, but for other countries could be two or three digits. To avoid one more validation and working with one big and complex problem, we can simplify it and divide one big problem into two small problems. Easy and neat solution is to just add separate drop down menu for country code, and keep phone text box for local phone number only.

Happy coding!


Tutorial toolbar:  Tell A Friend  |  Add to favorites  |  Feedback  |   


comments powered by Disqus