Regex to check the start of an international phone number

I am trying to create a regular expression that checks the first two characters of an international phone number when a user enters it into:

Valid: + , 0 , + followed by a number , 00 , 0032476382763 , +324763

Invalid: 0 followed by a number different than 0 , ++ , everything that is not in the valid list

So far I have come up with:

 /[0]|[00]|[+]|[+\d]]/g 

But this confirms ++ , but not +2 . The problem is that I cannot figure out how to check depending on the number of characters (1 or 2).

I use this expression in javascript . Here's the regex I was working on: http://regexr.com/3br5v

My regex level is not very good, so any help would be greatly appreciated.

+5
source share
4 answers

This seems like a trick (fixed bug with false positive 01 ):

 /^([+]|00|0$)(\d*)$/ 

https://regex101.com/r/qT0dB7/2

+2
source

The following template works for a large sample:

 ((?:\+(?!\+)|0(?:(?![1-9])))\d*) 

https://regex101.com/r/bL0uX9/2

0
source

This should work:

 ^[1-9]\d|\+[1-9]|00 
0
source

 \+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d| 2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]| 4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$ 

You can use if you want to confirm the full international number.

0
source

Source: https://habr.com/ru/post/1232014/


All Articles