How to validate a (country specific) phone number

DON'T USE A REGULAR EXPRESSION!!

There are too many variables for a regex to be of any use. Instead, just remove all characters from your string that are not 0-9, and then check to see if you have the correct number of digits left. Then it doesn't matter what extra stuff the user includes or doesn't include... ()x-+[] etc etc, as it just strips them all out and only counts the characters 0-9.

I've got a string extension that works great, and allows for a wide range of formats. It accepts an IsRequired parameter. So, you can validate a phone number like this:

string phone = "(999)999-9999"
bool isValidPhone = phone.ValidatePhoneNumber(true) // returns true

string phone ="1234567890"
bool isValidPhone = phone.ValidatePhoneNumber(true) // returns true

string phone = ""
bool isValidPhone = phone.ValidatePhoneNumber(false) // not required, so returns true

string phone = ""
bool isValidPhone = phone.ValidatePhoneNumber(true) // required, so returns false

string phone ="12345"
bool isValidPhone = phone.ValidatePhoneNumber(true) // returns false

string phone ="foobar"
bool isValidPhone = phone.ValidatePhoneNumber(true) // returns false

Here's the code (assumes a 10-digit American phone number. Adjust accordingly):

public static class StringExtensions
{

    /// <summary>
    /// Checks to be sure a phone number contains 10 digits as per American phone numbers.  
    /// If 'IsRequired' is true, then an empty string will return False. 
    /// If 'IsRequired' is false, then an empty string will return True.
    /// </summary>
    /// <param name="phone"></param>
    /// <param name="IsRequired"></param>
    /// <returns></returns>
    public static bool ValidatePhoneNumber(this string phone, bool IsRequired)
    {
        if (string.IsNullOrEmpty(phone) & !IsRequired)
            return true;

        if (string.IsNullOrEmpty(phone) & IsRequired)
            return false;

        var cleaned = phone.RemoveNonNumeric();
        if (IsRequired)
        {
            if (cleaned.Length == 10)
                return true;
            else
                return false;
        }
        else
        {
            if (cleaned.Length == 0)
                return true;
            else if (cleaned.Length > 0 & cleaned.Length < 10)
                return false;
            else if (cleaned.Length == 10)
                return true;
            else
                return false; // should never get here
        }
    }

    /// <summary>
    /// Removes all non numeric characters from a string
    /// </summary>
    /// <param name="phone"></param>
    /// <returns></returns>
    public static string RemoveNonNumeric(this string phone)
    {
        return Regex.Replace(phone, @"[^0-9]+", "");
    }
}

Jacek's regex works fine

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Enter a phone number.");
        string telNo = Console.ReadLine();                      
        Console.WriteLine("{0}correctly entered", IsPhoneNumber(telNo) ? "" : "in");    
        Console.ReadLine(); 
    }

    public static bool IsPhoneNumber(string number)
    {
        return Regex.Match(number, @"^(\+[0-9]{9})$").Success;
    }
}

Your regex should look like this, you need information about char counter

@"^(\+[0-9]{9})$"

Tags:

C#

Regex