Tuesday, January 8, 2013

Phone Number Validator Class


Below sample class validates phone number for counties USA, UK and Netherland. You can add the validations for other countries to the class.

public class PhoneValidator {

static IDictionary<string, Regex> countryRegex =
new Dictionary<string, Regex>() {
{ "USA", new Regex("^[2-9]\\d{2}-\\d{3}-\\d{4}quot;)},
{ "UK", new
Regex("(^1300\\d{6}$)|(^1800|1900|1902\\d{6}$)|(^0[2|3|7|8]{1}[0-
9]{8}$)|(^13\\d{4}$)|(^04\\d{2,3}\\d{6}$)")},
{ "Netherlands", new Regex("(^\\+[0-9]{2}|^\\+[0-
9]{2}\\(0\\)|^\\(\\+[0-9]{2}\\)\\(0\\)|^00[0-9]{2}|^0)([0-9]{9}$|[0-9\\-
\\s]{10}$)")},
};

public static bool IsValidNumber(string phoneNumber, string country) {
if (country != null && countryRegex.ContainsKey(country))
return countryRegex[country].IsMatch(phoneNumber);
else
return false;
}

public static IEnumerable<string> Countries {
get {
return countryRegex.Keys;
}
}
}

Using the class:

string ContactPhone="465 567 56";
string Country="USA";

if (!PhoneValidator.IsValidNumber(ContactPhone, Country))

{
  //Validation Message
}

No comments:
Write comments
Recommended Posts × +