3

I am trying to create a regular expression to force users to enter only phone numbers of the format accepted by Twilio API. With prefix of form +40 (eg.: +40123456789) not 0040 and without delimiters. Only the character + and numbers.

I came up with this so far \+\d{10,}.

Is this a good one? If not can you please improve on it?

Thanks.

EDIT:

  • the phone number should contain the prefix of the country and the phone number
  • the prefix is not fixed to +40(Romanian prefix). That is just an example.
  • the prefix should start with + not with 00
  • the number should not have delimiters

Good example: +40123456789 Bad example: (074) 352-7819

Beniamin Szabo
  • 1,909
  • 4
  • 17
  • 26
  • Could you please share exact requirements and supply good and bad examples? I see no leading spaces. Is `+` obligatory? Minimum 10 digits? If yes, you can use `(?<=\W|^)\+?[1-9][0-9]{9,}(?=\W|$)`. See https://regex101.com/r/rI6gB9/1. – Wiktor Stribiżew Apr 15 '15 at 07:48

2 Answers2

4

Here is the minimal Regex validation as it has been provided in their documentation: ^\+[1-9]\d{1,14}$

Cfr: https://www.twilio.com/docs/glossary/what-e164#regex-matching-for-e164

gildniy
  • 3,528
  • 1
  • 33
  • 23
2

The pattern provided will expect to match a plus sign followed by 10 digits. If you string starts with letters or white spaces or any other character, that regular expression will still succeed.

If you want the following:

  1. Starts with +40
  2. Contains a total of 10 digits and plus sign).
  3. Nothing else from the above

The following should do the trick: ^\+40\d{8}$. An example is available here.

npinti
  • 51,780
  • 5
  • 72
  • 96