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!

1
  • Are you sure that's the right regex? That looks more like an email validator
    – Blorgbeard
    Jul 19, 2012 at 3:41

5 Answers 5

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.

7
  • Btw, \s will match any whitespace, not just a space character. OP might want to match any whitespace, but he did say "spaces". Jul 19, 2012 at 4: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* Jul 19, 2012 at 4:17
  • Thanks Eugene, nice one on the explanation too
    – MikeW
    Jul 19, 2012 at 6: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. Jul 20, 2012 at 5: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. Jul 20, 2012 at 6:29
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.

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
3
  • Nah, [\s\d]{10} would not work - it takes 10 characters, some of them spaces and some of them digits. Jul 19, 2012 at 3:51
  • @Ria You didn't test your code well enough. Console.WriteLine(Regex.Match(new string(' ', 10), @"([\s\d]{10})").Success); definitely prints true. Jul 19, 2012 at 4: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. Jul 19, 2012 at 4:09
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

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})$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.