Reputation: 773
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.
Upvotes: 5
Views: 233
Reputation: 488
\+(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 validate full an international number.
Upvotes: 1
Reputation: 7482
This seems to do the trick (fixed bug with false positive 01
):
/^([+]|00|0$)(\d*)$/
https://regex101.com/r/qT0dB7/2
Upvotes: 2
Reputation: 80657
The following pattern works for a large sample:
((?:\+(?!\+)|0(?:(?![1-9])))\d*)
https://regex101.com/r/bL0uX9/2
Upvotes: 0