Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to check beginning of international phone number

I am trying to create a regex that validates the 2 first characters of an international phone number as the user types it in:

Is valid: +, 0, + followed by a number, 00, 0032476382763, +324763

Is not valid: 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 validates ++ but not +2. The problem is that I can't figure out how to validate depending on the number of characters (1 or 2).

I am using that expression in javascript. Here's the regex I worked on: http://regexr.com/3br5v

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

like image 834
skirato Avatar asked Oct 19 '22 01:10

skirato


1 Answers

This seems to do the trick (fixed bug with false positive 01):

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

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

like image 119
Alexey Shein Avatar answered Nov 03 '22 17:11

Alexey Shein