A vanity number spells a word with keypad letters, so 1-800-FLOWERS is 1-800-356-9377.
dotnet add package RecursiveAI.OneContactRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneContact; var phone = PhoneNumber.Parse( "1-800-FLOWERS", Region.US); Console.WriteLine(phone.E164); bool vanity = phone.Flags .HasFlag(PhoneFacts.Vanity); Console.WriteLine($"Vanity: {vanity}");
Unzip the example, then dotnet run.
Prints the international number and whether the input was a vanity number:
+18003569377 Vanity: True
Region.US tells the parser how to interpret the number without a leading +. If the text can’t be parsed, Parse throws ContactParseException; use TryParse for a phone number entered in a form.
Variations
Take an international number without a region hint. Replace the Parse call with this one. The leading +1 supplies the calling code, and lowercase letters give the same output.
var phone = PhoneNumber.Parse("+1-800-flowers");Related recipes
The short answer
Pass 1-800-FLOWERS and Region.US to PhoneNumber.Parse. Read E164 to get +18003569377, a + followed by the country calling code and the national number, without separators. Read PhoneFacts.Vanity if you need to know whether the original text was a vanity number. The all-digit form of the same number has the same E164 string but does not have the vanity flag.
How do I convert letters to numbers on a phone keypad?
A phone keypad maps letters in groups: ABC to 2, DEF to 3, GHI to 4, JKL to 5, MNO to 6, PQRS to 7, TUV to 8, and WXYZ to 9. Read FLOWERS from left to right: F is 3, L is 5, O is 6, W is 9, E is 3, R is 7, and S is 7. That gives 3569377.
OneContact’s phone parser converts letters to keypad digits when the phone number contains at least three ASCII letters. It reads ext before an extension number as a marker, not as part of the phone number. This is why globally replacing every letter in a contact field with a digit is not equivalent to parsing a phone number: it would turn part of the extension marker into part of the number.
How do I format a vanity phone number internationally in C#?
Use E164 for the international number and keep the original spelling separately if you want to display 1-800-FLOWERS to users. The digits cannot tell you which letters were supplied: 3 could have come from D, E, or F, or have been a digit all along. The vanity flag records how the number was entered, not whether the number can receive a call.
For input without + and a calling code, use the country or region the user selected. Without a hint, OneContact refuses national input rather than assuming it is American. If you accept extensions, keep Extension separately or read Canonical, which appends it as ;ext=…. E164 as printed above omits the extension.