Database Search Solution
(New Version) Search Control
Free ASP.NET Controls
ASP.NET Media Player Control
Flash Video Player Control
ASP.NET Telecommute Jobs
Free IP Location Lookup
Test .Net Regular Expressions
CSS/Table/DIV Page Layouts
Article Sites Master List
.NET Windows Forms
General .NET Framework
Accepting Credit Cards
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:
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# ]
[ VB.NET ]
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:
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.
comments powered by Disqus