-1

I want to allow the user to enter only patterns like:

+9720545455454

056565656345

03-43434344

0546-4234234

*9090

+97203-0656534

Meaning, I don't want to allow the user to gibberish everything together, like:

+954-4343+3232*4343+-

+-4343-+5454+9323+234

How can I fixed this pattern

public static bool IsPhoneNumberCorrect(string phoneNumber)
{
    return Regex.IsMatch(phoneNumber, @"^[0-9*+-]+$");
}

for that purpose?

Community
  • 1
  • 1
JAN
  • 21,236
  • 66
  • 181
  • 318
  • do you allow for `+` signs inside the phone number or only at the beginning? – Zohar Peled Sep 08 '15 at 09:08
  • @ZoharPeled: Only at the beginning . – JAN Sep 08 '15 at 09:09
  • and only one hyphen is allowed? is `972-3-5551234` allowed? – Zohar Peled Sep 08 '15 at 09:12
  • 1
    Any reason you're not using something like [GitHub: libphonenumber](https://github.com/erezak/libphonenumber-csharp) ? – Lasse V. Karlsen Sep 08 '15 at 09:13
  • 2
    possible duplicate of [A comprehensive regex for phone number validation](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation) – Sayse Sep 08 '15 at 09:13
  • 2
    "patterns like " is *not* a specification. You need to be more specific in what validity means here. That means listing the rules explicitly rather than having us trying to figure them out from a few samples. – paxdiablo Sep 08 '15 at 09:13
  • Many countries use spaces rather than hyphens. Additionally, area codes are often in parentheses. E.g. in the UK this is normal: `+44 (1632) 960 239` – Ben Sep 08 '15 at 09:16
  • @Ron: Nobody objects to people that are learning. Your phrasing of the question showed no evidence that you had read such docs. If you had complained about a specific issue that you didn't understand, that would have been one issue. Otherwise this is "gimme the codez". – Ira Baxter Sep 08 '15 at 09:26
  • Rather than restrict what the user may type, you want to strip out any special characters other than the initial +, then format the number to the traditional format of the country. At the very least you should have a list of valid country codes so e.g. America/Canada `+1 555123456`, UK: `+44 1632960239` and so on. List of valid country codes are on Wikipedia. Portugal: `+351 000000000` and so on. – Ben Sep 08 '15 at 09:27

3 Answers3

5

If you do not care about digit group length, you can allow + or * only at the beginning, and then match initial digits and then optional groups of hyphen+digits:

return Regex.IsMatch(phoneNumber, @"^[+*]?\d+(?:-\d+)*$");

See demo

Note you can limit the number of hyphen+digit with a quantifier. Say, there can be none or 1:

^[+*]?\d+(?:-\d+)?$"
                 ^

See another demo

And in case there can be more than 1, use a limiting quantifier:

^[+*]?\d+(?:-\d+){0,3}$"
                 ^^^^^

Here, {0,3} means 0, 1, 2 or 3 repetitions of the hyphen+digits group.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
2
^(?!(.*\+){2})(?!(.*-){2})(?!(.*\*){2})[0-9*+-]+$

YOu can use lookaheads to make sure special characters appear only once.See demo.

https://regex101.com/r/vV1wW6/1

vks
  • 67,027
  • 10
  • 91
  • 124
0

What you first need to do is identify what exactly the pattern is. This doesn't need to be in code. In you example, I see a leading character, followed by a first number group, followed by an optional dash and second number group. The leading character can be +, * or 0. The number groups are one digit between 1 and 9 followed by one or more digits 0 to 9. Translating each element gives:

Leader: [+*0]

Dash: -

Number group: [1-9][0-9]+

Throwing everything together you get

[\+\*0][1-9][0-9]+(-[1-9][0-9]+)?

Some groups may have minimum and maximum lengths, you can still work that in changing + to {min, max}.

Martijn
  • 11,964
  • 12
  • 50
  • 96