0

I'm trying to build a regex to verify in JS if a international number is well formated.

I consider a number is formated as follow : '00'[country][number]. The 'country' and the 'number' are simply numbers (which can't start with 0). The 'country' have size {1-3}, and the 'country' + 'number' are < 15.

I use this wrong regex : /^00[1-9][0-9]{0,2}[1-9][0-9]{0,11}$/ My problem is about the size of 'number' that depend of 'country' size. The max size of 'number' is 15-'country'.size

How can I do that ?

I see What regular expression will match valid international phone numbers? but number size is fixed to 15, not depending on country size. And a generic way (not giving all the countries codes) should be better, if possible.

Here some example that have to match with the regex

  • 00111111111111111
  • 001100
  • 00111111111111111
  • 00100100000000000
  • 00101000000000000 (my regex do not match it, because in this case 'country' = 2 and 'number' = 13)

and not to match with

  • 00100010000000000 ('country' = 4)

Thx a lot

Community
  • 1
  • 1
Clampu
  • 51
  • 7
  • 2
    (and a lot of others) – Thomas Ayoub Nov 19 '15 at 15:55
  • I saw that, but I prefer (if possible) to have a generic way, not giving the list of country codes. Moreover, the 'number' max size is fixed at 15 in theese exemples, no depending on country size. – Clampu Nov 19 '15 at 16:06
  • **0020**10000000 is valid whereas **00201**0000000 is not. But this is the same input. How do you check that then? – Thomas Ayoub Nov 19 '15 at 16:10
  • I don't understant what you mean Thomas sorry. If we find at least one way where the regex match with the model (the first input you give), so the result is OK, 002010000000 match. But if there is no way testing all possibilities, it's ko. – Clampu Nov 19 '15 at 16:14
  • I meant that if the country code is 20 and the phone number is 1000000 it's a valid number, but if the code country is 201 & the number 000000 it has the same representation 201000000 but it's valid in one case but not the other – Thomas Ayoub Nov 19 '15 at 16:16
  • Yes, I get it, so this number is possibly right (and possibly wrong). if we consider that we don't know if the code is 20 or 201. I want my regex validate this number because it can be ok. – Clampu Nov 19 '15 at 16:21
  • `'number' = 13` => you mean that number should be <=12 ? – Thomas Ayoub Nov 19 '15 at 16:25
  • I did a mistake, I edited. 00101000000000000 is well formated but my regex do not detect if beacause I considere that 11 is the max of number (15-max(country)). – Clampu Nov 19 '15 at 16:34
  • so change the end of pattern to `{0,12}$` instead of `{0,11}$`. ? – M.kazem Akhgary Nov 19 '15 at 16:39
  • I can't, because of 001001000000000000 : 3 + 13 = 16 – Clampu Nov 19 '15 at 16:41
  • after million times of edit i fixed the pattern in my answer. xD sorry for confusion. now you can try it. if you have any questions or problems feel free to ask. – M.kazem Akhgary Nov 19 '15 at 18:06

2 Answers2

0

Because I don't know the size of 'country', I suppose all the possibles cases (0, 1 or 2) and I deduce the 'number' size. :

^00[1-9][1-9][0-9]{0,13}$|^00[1-9][0-9][1-9][0-9]{0,12}$|^00[1-9][0-9]{2}[1-9][0-9]{0,11}$
karthik manchala
  • 13,492
  • 1
  • 31
  • 55
Clampu
  • 51
  • 7
0

Use this pattern

^00(?=[1-9]\d{0,2}[1-9])(?=\d{2,15}$)\d+$

Here is the explanation

^00 string should start with 00.

(?=[1-9]\d{0,2}[1-9]) ensures that we have a non zero digit after maximum of two digits.

(?=\d{2,15}$) ensures that we at least have 2 digits and maximum 15 digits which is the minimum and maximum length of country and number.

\d+ matches all remaining digits.

M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118