28

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})$/
3
  • What about home phone numbers and cellphones beginning with 07? Jul 20, 2016 at 14:24
  • ^(((\+33\s)|0)[1-9]\s([0-9][0-9]\s){4})$ may do the trick but it's quite complex and is just and modification from yours (obligation to put space between two digits), but 0033 isn't allowed?
    – Larme
    Jul 20, 2016 at 14:27
  • @Larme it is, indeed Jul 20, 2016 at 14:28

10 Answers 10

60

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

2
  • 1
    JavaScript example : '+33678787878'.match(/^(?:(?:\+|00)33|0)\s*[1-9](?:[\s.-]*\d{2}){4}$/gmi) Apr 7, 2020 at 9:42
  • Compatible with JSON schema: ^((\\+|00)33|0) *[1-9]([ .-]*[0-9]{2}){4}$
    – n0p
    Jun 2, 2020 at 8:40
20

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:

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

It also takes into account the 0033 scenario.

2
  • why not ^(\+33|0|0033)[1-9]\d{8}$ controlling for 8 numbers instead of 4 times 2 numbers (\d{2}){4}? Also if there is no reason to capture, you can dump all but the first group of parens.
    – Buzut
    Mar 29, 2019 at 14:25
  • In addition, in JavaScript, if using new RegExp, to avoid SyntaxError: nothing to repeat, you'll have to double escape the "+" at the beginning. So the final regex would be ^(\\+33|0|0033)[1-9][0-9]{8}$.
    – Buzut
    Mar 29, 2019 at 14:26
3

Try this:

/^(\+33 |0)[1-9]( \d\d){4}$/
1

Split the regex into two separate parts:

  • the prefix that can either be +33 X or 0X

  • the rest of the number (XX XX XX XX)

Regex would be:

^((?:\+33\s|0)[1-9](?:\s\d{2}){4})$
   ^ non-capturing group for prefix
                      ^ non-capturing group for number
 (                                 )
 ^ actual capture group from your original regex

This only allows whitespace as a separator; if you want something more open, Thomas Ayoub's answer is more verbose.

tested on Regex101

NOTE: As per Thomas's comment, since the regex is a full match using the start and end tokens (^$), the capture group is rather unnecessary. You can then take it out to look like this:

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

and it should work fine.

2
  • Thanks for the credits. As the string is a full match (^$) I'm not sure that the capturing group is necessary. Also, +33 0 12 34 56 78 is not a valid number... Jul 20, 2016 at 14:43
  • @ThomasAyoub Ah, my lack of knowledge of how phone numbers works is to blame for that. Will edit. And very true about the capture group, I just wanted to reflect OP's original intent. Will make a note of it.
    – R Nar
    Jul 20, 2016 at 14:45
1

RegEx for format: 0X XX XX XX XX +33 X XX XX XX XX

^(0|\+33 )[1-9]([-. ]?[0-9]{2} ){3}([-. ]?[0-9]{2})$
1

In a java based application, you can use this dependency to validate phone numbers:

    <dependency>
         <groupId>com.googlecode.libphonenumber</groupId>
         <artifactId>libphonenumber</artifactId>
    </dependency>

Example of use:

  public static boolean isValidFrenchPhone(String portalPhone) {
    try {
        PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
        Phonenumber.PhoneNumber number = phoneUtil.parse(portalPhone, "FR");
        return phoneUtil.isValidNumberForRegion(number, "FR");
    } catch(Exception e) {
        return false;
    }
}
0

I know this has been a long time since the question was asked but I tried couple of solutions in that post. None of them satisfied my requirements so I came up with this custom/heavy one that does the job for France number (in any format, I think) and also Belgium phone numbers.

    def phone_pattern():
        """
                \+\d{2}[-.\s]?(?:\(0\)|0)*?[-.\s]?          # Dialing code +CC00(0) or +CC00 or +CC00 0
                00[-.\s]?\d{2}[-.\s]?(?:\(0\)|0)*?[-.\s]?   # Dialing code 0033 \(0)|0
                (?:\(0\)|0|)\d?[-.\s]?                      # First number (from 1 to 9) preceeded by a non mandatory 0 or (0)
                (?:\d{2}[-.\s]?){3,4})                      # End of the phone number (3 or 4 times 2 following digits separated or not by .- )
                (?:$|\D)                                    # Anything but a number or the end of string    
        :return:
        """
        # To be improved to make the 0 mandatory when +33 or 00 is missing. Ex it will match 234558899
        # return re.compile('((?:\+\d{2}|00[-.\s]?\d{2})?[-.\s]?(?:\(0\)|0|)\d?[-.\s]?(?:\d{2}[-.\s]?){3,4})')
        # This version should tka
        return re.compile('((?:\+\d{2}[-.\s]?(?:\(0\)|0)*?[-.\s]?|00[-.\s]?\d{2}[-.\s]?(?:\(0\)|0)*?[-.\s]?|0)\d?[-.\s]?(?:\d{2}[-.\s]?){3,4})(?:$|\D)')
0

+33 only is for metropolitan France. I think you can also add overseas France with

/^(?:(?:\+|00|0)((262|692)|(263|693)|508|(5|6)90|(5|6)94|(5|6|7)96|681|687|689))(?:[\s.-]*\d{2}){3,4}$/

If you consider digits length difference between overseas territories as well as some specific area codes for mobile, the regex becomes really messy...

The function I'm currently using in JS :

export const isValidFrenchPhoneNumber = (phonenumber) => {
    const metropolitanFranceReg = new RegExp(/^(?:(?:\+|00)33|0)\s*[1-9](?:[\s.-]*\d{2}){4}$/)
    const overseasFranceReg = new RegExp(/^(?:(?:\+|00|0)((262|692)|(263|693)|508|(5|6)90|(5|6)94|(5|6|7)96|681|687|689))(?:[\s.-]*\d{2}){3,4}$/)
    // src: https://en.wikipedia.org/wiki/Telephone_numbers_in_France
    // 262, 263 = La Réunion, Mayotte and IO territories ; 508 = Saint-Pierre-et-Miquelon
    // 590 = Guadeloupe, Saint-Martin et Saint-Barthélemy ; 594 = French Guiana (Guyane) ; 596 = Martinique
    // 681 = Wallis-et-Futuna ; 687 = Nouvelle-Calédonie
    // 689 = French Polynesia
    return !(phonenumber.match(metropolitanFranceReg) === null) || !(phonenumber.match(overseasFranceReg) === null)
}
0

Here is a version compatible with a lot of OCR imperfections (for example for scanned restaurant receipts):

"(\s*0\s*[1-9]\s*\.?\s*([\s.-]*\d\s*){8})"gmix

or Python flavor version with a group:

(?P<french_number>\s*0\s*[1-9]\s*\.?\s*([\s.-]*\d\s*){8})

working examples:

06 01 02 03 04
0 6 01 02 0 3 04
 0 7 01 02 03 04
 0 7 .0   1. 02 .03. 04
0 7.01.02.0 3.04
0 7-01 - 02-0 3    - 04

The international prefix has been omitted.

Make sure you remove spaces with a string replace call for each group.

Regex sandbox here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.