Baptiste Arnaud
Baptiste Arnaud

Reputation: 2750

Regex for French telephone numbers

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})$/

Upvotes: 30

Views: 87423

Answers (10)

myselfhimself
myselfhimself

Reputation: 579

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

Upvotes: 0

Thibault
Thibault

Reputation: 434

+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)
}

Upvotes: 0

user7375303
user7375303

Reputation:

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;
    }
}

Upvotes: 1

Thomas AMET
Thomas AMET

Reputation: 81

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)')

Upvotes: 0

amamou nesrine
amamou nesrine

Reputation: 13

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})$

Upvotes: 1

Hugo Trial
Hugo Trial

Reputation: 396

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

It also takes into account the 0033 scenario.

Upvotes: 6

Ronan Kerdudou
Ronan Kerdudou

Reputation: 318

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:

Upvotes: 22

Thomas Ayoub
Thomas Ayoub

Reputation: 29451

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

Upvotes: 64

R Nar
R Nar

Reputation: 5515

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.

Upvotes: 1

logi-kal
logi-kal

Reputation: 7880

Try this:

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

Upvotes: 3

Related Questions