Wednesday, January 14, 2009 1:44 PM
Here is a simple function that validates a U.S phone number using regular expressions. Note the use of the static method, which prevents unnecessary object instantiation. Its all about optimize code.
public static bool IsValidPhoneNumber(string phoneNumber)
{
string pattern = @"^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$";
return Regex.IsMatch(phoneNumber, pattern);
}