How strict must the regex be?
For the above given numbers, I'd go for a pattern match as follows:
ukphone = [ prefix ] suffix | special .
prefix = international | zero .
suffix = first part part part part part part part part part .
special = group part { part } .
international = '+' first part .
group = '(' first { part } { ' ' } ')' .
first = { ' ' } digit .
part = { ' ' | '-' } digit .
digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' .
zero = '0' .
You may now translate this into a Regex.
First pseudo-code:
^\s*((?:[+]<first><part>|0)?<first><part>{9}|[(]<first><part>+\s*[)]<part>+)\s*$
and replace
<first>
and
<part>
by
first: (?:\s*\d)
part: (?:[-\s]*\d)
This is a rather relaxed matcher:
- several separators in a row is accepted
- special numbers are not checked for proper length
- ...
If you want disallow multiple separators in a row, use:
first: (?:\s?\d)
part: (?:[-\s]?\d)
Have fun!
Cheers
Andi
[EDIT]
Initially the question was tagged C#3.5, so my suggested solution was for C#.
Now it is tagged JQuery.
Nonetheless: the following Regex works for C# and will probably too for JQuery:
Regex rex = new Regex(@"^\s*(([+]\s?\d[-\s]?\d|0)?\s?\d([-\s]?\d){9}|[(]\s?\d([-\s]?\d)+\s*[)]([-\s]?\d)+)\s*$");
string[] data = new string[]
{ "0116 2746 190"
, "0116 2746190"
, "079155 24272"
, "+44 78904 12345"
, "1234567890"
, "(0800) 123 456"
, "0116 2746190"
};
foreach (string number in data)
{
Console.WriteLine("match({0}) = {1}", number, rex.Match(number).Success);
}
[/EDIT]