1

I am trying to use regular expressions in JavaScript to test if the value of a form text input is a French mobile phone number.

French mobile phone numbers begin with 06 or 07 and are followed by 8 digits, like these : 0611223344 or 0744332211. The international form of these numbers begin with +33 which replaces the first 0, like these : +33611223344 or +33744332211.

I am using this RegEx : (\+\b33|\b0)[67][0-9]{8}\b, which tests if the input begins with +336/7 or 06/7 and is followed by 8 digits.

I added the Word Boundary (\b) so numbers like 06112233440611223344 (two concatenated correct forms) don't match.

This RegEx works fine for thoses cases :

0611223344 // matches
0744332211 // matches
+33611223344 // matches
+33744332211 // matches
06112233440744332211 // doesn't match

But I still encounter problems with these inputs :

+33611223344+33611223344
0611223344 0744332211
0744332211+33611223344
0744332211 +33611223344

They all match...

I tried to add ^ and $ in all possible ways to my RegEx, but whenever I add one of these symbols, the RegEx doesn't work anymore.

Seeven
  • 969
  • 1
  • 8
  • 24

2 Answers2

1

You could use the below regex to match all the French phone numbers,

^(?:(?:0|\+33)(?:6|7))\d{8}$

DEMO

Use the below regex if you want to match the numbers which are in this format 06 21 05 65 48 and +33 6 21 05 65 48 also,

^(?:(?:(?:0|\+33)(?:6|7))\d{8}|0[67](?: \d{2}){4}|\+33 [67](?: \d{2}){4})$

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • Should be great to accept these form too: 06 21 05 65 48 and +33 6 21 05 65 48 – labilbe Sep 04 '14 at 09:11
  • @labilbe why? op didn't mention this. – Avinash Raj Sep 04 '14 at 09:14
  • Because french users tend to input numbers like this too. So, your regex can serve for a better input check. – labilbe Sep 04 '14 at 09:15
  • Indeed I didn't mention it, but this is a common form of writing French numbers too. I don't know if the spaced form should be accepted by the form though. – Seeven Sep 04 '14 at 09:20
  • @AvinashRaj: Can you explain me why you are using "Non-capturing groups" `(?:)` ? – Seeven Sep 04 '14 at 09:21
  • @SeevenByakko for only matching purposes, use non-capturing groups. If you want to capture something then use capturing groups. – Avinash Raj Sep 04 '14 at 09:23
  • You rock Avinash! Thank you very much! – labilbe Sep 04 '14 at 09:38
  • @AvinashRaj: Thanks for the spaced-form proposition, I refactorised it a little : `^(?:(?:0|\+33)[67]\d{8}|(?:0[67]|\+33 [67])(?: \d{2}){4})$`. I don't know yet if the spaced-form should be accepted though, but I keep this form in note. – Seeven Sep 04 '14 at 09:38
  • @AvinashRaj: I like the use of ` ?` but it makes these match : `0 611223344` and `+33 611223344`. – Seeven Sep 04 '14 at 10:16
  • @AvinashRaj: These work ^^ : `0 6 11 22 33 44` and `+336 11 22 33 44` – Seeven Sep 04 '14 at 13:13
0
^(\+\b33|\b0)[67][0-9]{8}$

Your regex works fine.Just add the anchors.

See demo.

http://regex101.com/r/pP3pN1/35

vks
  • 67,027
  • 10
  • 91
  • 124
  • Thanks for your demo. It turned out the website I was testing my RegEx (regexr.com), doesn't consider new lines as separated inputs... – Seeven Sep 04 '14 at 09:23
  • ERRATUM: regexr.com does consider new lines as long as you set the `/ /gm` tags (and not only `/ /g` like default). (I still think regex101 is better though). – Seeven Sep 04 '14 at 10:20
  • @SeevenByakko /m us multiline.you have to set it even on regex101.com if you want to test it over multiple lines.\g ifs for global match.So that it returns all match.flags have to be set everywher. – vks Sep 04 '14 at 10:22