Products
Database Search Solution (New Version) Search Control SEO Pager Highlighter Shortcut Controls Crypt Package Free ASP.NET Controls
Geotargeting Component ASP.NET Media Player Control Flash Video Player Control Services
ASP.NET Telecommute Jobs Free IP Location Lookup Test .Net Regular Expressions CSS/Table/DIV Page Layouts Custom Programming Article Sites Master List |
Function to Format String As Phone NumberVery 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 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 ExamplesDifferent 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:
Format Phone Number function remarksThis 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 |