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();
C#
x
14
1
Regex regValidUKNumber = new Regex(@"^\s*(([+]\s?\d[-\s]?\d|0)?\s?\d([-\s]?\d){9}|[(]\s?\d([-\s]?\d)+\s*[)]([-\s]?\d)+)\s*$");
2
string[] inputData = new
3
string[]
4
{ "0116 1111111"
5
, "07777777777"
6
, "7777777777"
7
, "+44 7777777777"
8
, "(0800) 111 111"
9
};
10
foreach (string telephoneNumber in inputData)
11
{
12
Console.WriteLine("match({0}) = {1}", telephoneNumber, regValidUKNumber.Match(telephoneNumber).Success);
13
}
14
Console.ReadLine();