Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for French telephone numbers

Tags:

regex

I'm trying to implement a Regex that allows me to check if a number is a valid French telephone number.

It must be like this:

0X XX XX XX XX

or:

+33 X XX XX XX XX

Here is what I implemented but it's wrong ...

/^(\+33\s[1-9]{8})|(0[1-9]\s{8})$/
like image 393
Baptiste Arnaud Avatar asked Jul 20 '16 14:07

Baptiste Arnaud


3 Answers

You can use:

^
    (?:(?:\+|00)33|0)     # Dialing code
    \s*[1-9]              # First number (from 1 to 9)
    (?:[\s.-]*\d{2}){4}   # End of the phone number
$

See demo


It allows whitespaces or . or - as a separator, or no separator at all

like image 159
Thomas Ayoub Avatar answered Nov 17 '22 11:11

Thomas Ayoub


A complex example (the one I'm using):

^(?:(?:\+|00)33[\s.-]{0,3}(?:\(0\)[\s.-]{0,3})?|0)[1-9](?:(?:[\s.-]?\d{2}){4}|\d{2}(?:[\s.-]?\d{3}){2})$

For example it matches each of these lines:

0123456789
01 23 45 67 89
01.23.45.67.89
0123 45.67.89
0033 123-456-789
+33-1.23.45.67.89
+33 - 123 456 789
+33(0) 123 456 789
+33 (0)123 45 67 89
+33 (0)1 2345-6789
+33(0) - 123456789

More:

  • Test it
  • Visualize it
like image 32
Ronan Kerdudou Avatar answered Nov 17 '22 13:11

Ronan Kerdudou


var phoneExp = /^((\+)33|0|0033)[1-9](\d{2}){4}$/g;

It also takes into account the 0033 scenario.

like image 6
Hugo Trial Avatar answered Nov 17 '22 12:11

Hugo Trial