9

I have a phone number field with the following regex:

[RegularExpression(@"^[0-9]{10,10}$")]

This checks input is exactly 10 numeric characters, how should I change this regex to allow spaces to make all the following examples validate

1234567890
12 34567890
123 456 7890

cheers!

Rob
  • 4,927
  • 12
  • 49
  • 54
MikeW
  • 4,749
  • 9
  • 42
  • 83

5 Answers5

16

This works:

^(?:\s*\d\s*){10,10}$

Explanation:

^ - start line
(?: - start noncapturing group
\s* - any spaces
\d - a digit
\s* - any spaces
) - end noncapturing group
{10,10} - repeat exactly 10 times
$ - end line

This way of constructing this regex is also fairly extensible in case you will have to ignore any other characters.

Eugene Ryabtsev
  • 2,232
  • 1
  • 23
  • 37
  • Btw, `\s` will match any whitespace, not just a space character. OP might want to match any whitespace, but he did say "spaces". – Michael Graczyk Jul 19 '12 at 04:10
  • @Michael Graczyk - good point; `\s` will indeed match various sorts of spaces (not sure if all, like zero-width space and other fancy spaces). If only some of them are allowed, like only the ` ` character you get by pressing spaceber, might list them like `[ ]*` or ` *` instead of `\s*` – Eugene Ryabtsev Jul 19 '12 at 04:17
  • Thanks Eugene, nice one on the explanation too – MikeW Jul 19 '12 at 06:27
  • This solution counts the spaces as part of the 10 characters. Counting only the numbers is considerably harder and messier with a pure regex approach. – Dominic Cronin Jul 20 '12 at 05:18
  • @Dominic Cronin - in fact, it counts groups `(?:)` of `\s*\d\s*`. With slight modification it could count any other groups. Currently, one group contains exactly one digit, which gives 10 groups of 1 digit each and an unknown number of whitespace of unknown type. – Eugene Ryabtsev Jul 20 '12 at 06:29
  • Indeed - so as I said, instead of coming up with a total of 10 digits, optionally interspersed with spaces, it matches 10 of either digit or space. Doing that is simply a matter of adding a space to the character class in the original regex, which isn't exactly difficult. However, the problem as stated is much more difficult, and I would probably not solve it with a pure regex solution. – Dominic Cronin Jul 20 '12 at 21:42
  • @EugeneRyabtsev - I've just re-read your solution, and I'm wrong. Your group will indeed match a single digit and any surrounding whitespace, and this group must occur exactly 10 times. Assuming that spaces are allowed both before and after the phone number, this will work. – Dominic Cronin Jul 29 '12 at 16:57
1

Use this simple regex

var matches = Regex.Matches(inputString, @"([\s\d]{10})");

EDIT

var matches = Regex.Matches(inputString, @"^((?:\s*\d){10})$");

explain:

   ^             the beginning of the string

  (?: ){10}      group, but do not capture (10 times):

  \s*            whitespace (0 or more times, matching the most amount possible)

  \d             digits (0-9)

  $              before an optional \n, and the end of the string
Ria
  • 10,237
  • 3
  • 33
  • 60
  • Nah, `[\s\d]{10}` would not work - it takes 10 characters, some of them spaces and some of them digits. – Eugene Ryabtsev Jul 19 '12 at 03:51
  • @Ria You didn't test your code well enough. `Console.WriteLine(Regex.Match(new string(' ', 10), @"([\s\d]{10})").Success);` definitely prints `true`. – Michael Graczyk Jul 19 '12 at 04:04
  • Still wrong: `Console.WriteLine(Regex.Match(new string('0', 11), @"((?:\s*\d){10})").Success);` prints `true`. You need to check for the beginning and end of the string. See Eugene's answer. – Michael Graczyk Jul 19 '12 at 04:09
1

Use this:

^([\s]*\d){10}\s*$

I cheated :) I just modified this regex here:

Regular expression to count number of commas in a string

I tested. It works fine for me.

Community
  • 1
  • 1
Jason Antic
  • 116
  • 3
1

Depending on your problem, you might consider using a Match Evaluator delegate, as described in http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchevaluator.aspx

That would make short work of the issue of counting digits and/or spaces

Dominic Cronin
  • 6,062
  • 2
  • 23
  • 56
0

Something like this i think ^\d{2}\s?\d\s?\d{3}\s?\d{4}$

There are variants : 10 digits or 2 digits space 8 digits or 3 digits space 3 digits space 4 digits.

But if you want only this 3 variants use something like this

^(?:\d{10})|(?:\d{2}\s\d{8})|(?:\d{3}\s\d{3}\s\d{4})$
ForEveR
  • 55,233
  • 2
  • 119
  • 133