Php regex for lebanese phone number

I am writing a php application that asks people for their phone number in Lebanon. I don’t want to be very strict in the input format, so I run into some validation problems.

Lebanese phone number looks like this.

961 3 123456

961 : country code. I want it to be valid with or without it.

3 : area code. here where difficult. possible code codes are 03, 70 and 71. When a country code is present, 03 drops to 0 and becomes 3, and 70 and 71 are equal with or without a country code.

123456 : phone number, always 6 digits.

Here are the formats I'm trying to check:

961 3 123456
961 70 123456
961 71 123456
03 123456
70 123456
71 123456

spaces here are just for clarity, I check after removing all spaces and non-alphanumeric characters.

which would be great if someone can help. thank

+5
source share
3 answers

I'm sure there is a way to smooth out, but

^(961(3|70|71)|(03|70|71))\d{6}$

seems to work, assuming I understand the requirements.

+6
source

^((961)?(7(0|1))|(961|0)3)[0-9]{6}$

This is a composition of these three regular expressions:

(961)?(7(0|1))  // 70 and 71 prefixes
(961|0)3        // (0)3 prefix
[0-9]{6}        // main number
+5
source

Providing spaces or common delimiters (although I know you said you weren’t bothered):

^((961[\s+-]*(3|7(0|1)))|(03|7(0|1)))[\s+-]*\d{6}$

http://regexr.com?2t89v

+1
source

All Articles