0

I am trying to write a regular expression that matches theese cases.

Phone number can be: 8 digits 0-9 OR 12 digits 0-9 OR 12 digits 0-9 and a + sign So: 12345678, 0012345678, +0012345678 are valid options

[RegularExpression("^[0-9]{8})|[0-9]+{12}|[0-9]{11}$", ErrorMessage = "Invalid phone")]

Would also be nice that on the + validation case, the plus sign have to be in the beginning (following 10 digits) and on the 12 digits validation there have to be 00 first (then following 10 digits)

tereško
  • 58,060
  • 25
  • 98
  • 150
stibay
  • 1,200
  • 6
  • 23
  • 44
  • Have you taken a look at http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation – nhahtdh Oct 13 '14 at 08:01
  • @stibay You mean there may be `+` sign before the number? – Avinash Raj Oct 13 '14 at 08:21
  • Yes, if you have a + sign. It have to be in the beginning and following 10 digits. F.ex +4712345678 – stibay Oct 13 '14 at 08:29
  • Almost got it working now. [RegularExpression(@"^\d{8}|00\d{10}|\+\d{2}\d{8}", ErrorMessage = "Invalid Phone")] Only the middle part is not working. The 00XXXXXXXXXX is still invalid :/ – stibay Oct 13 '14 at 08:34
  • put them all into a non-capturing group, like `@"^(?:\d{8}|00\d{10}|\+\d{2}\d{8})$"` – Avinash Raj Oct 13 '14 at 08:40

3 Answers3

1

You could try the below regex,

@"^(?:\d{8}|00\d{10}|\+\d{2}\d{8})$"
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0
[0-9]{8}|00[0-9]{10}|/+00[0-9]{11}
Vajura
  • 1,112
  • 7
  • 16
  • I get an error on the + sign. Unrecognized escape sequence – stibay Oct 13 '14 at 08:07
  • @stibay whoops sry, in C# thats \ is the escape sequence, try it now with / as the escape sequence – Vajura Oct 13 '14 at 08:19
  • I am using C#, I figgured out that it was because I neede at @ at the start to allow for using \ as the escape character. Other option was to double \\ – stibay Oct 13 '14 at 08:20
  • The regex looks like this. But it still doesnt work correct. [RegularExpression(@"^[0-9]{8})|00[0-9]{12}|\+00[0-9]{11}", ErrorMessage = "Invalid Phone")] 004712345678 and +4712345678 says that it is invalid. Only 12345678 works. – stibay Oct 13 '14 at 08:21
0

Try this regex:

[RegularExpression("^(\+)?(\d{2})?\d{8}$", ErrorMessage = "Invalid phone")]
Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93