New! GraphQL.NET Cookbook by Senthil Kumar Balu

Regular Expression and UK Telephone Number Validation in C#

Here’s simple code snippet demonstrating how to validate a UK Telephone number (both Mobile and landline number) using regular expression in C#.

Regular Expression and UK Telephone Number Validation in C#

Regex regValidUKNumber = new Regex(@"^\s*(([+]\s?\d[-\s]?\d|0)?\s?\d([-\s]?\d){9}|[(]\s?\d([-\s]?\d)+\s*[)]([-\s]?\d)+)\s*$");
string[] inputData = new
string[]
{ "0116 1111111"
, "07777777777"
, "7777777777"
, "+44 7777777777"
, "(0800) 111 111"
};
foreach (string telephoneNumber in inputData)
{
Console.WriteLine("match({0}) = {1}", telephoneNumber, regValidUKNumber.Match(telephoneNumber).Success);
}
Console.ReadLine();
Senthil Kumar B
Senthil Kumar is a former Microsoft MVP (Most Valuable Professional). He is a Co-Author of the book "Windows 10 Development Recipes using JavaScript and CSS" for Apress Publication. He is a technical presenter, blogger, mentor and a Geek.  Senthil is a regular speaker is various local user groups. He has presented at conferences like Great Indian Developer Summit (GIDS) & Microsoft DevCamps. You can reach out to him via his Twitter handle @isenthil.