i am writing a php app that asks people for their phone number in lebanon. I don't want to be very strict in the entry format so i'm running into some trouble validating.
A lebanese phone number looks like that.
961 3 123456
961: the country code. i want it to be valid with or without it.
3: the area code. here's where it is tricky. the possible area codes are 03, 70 and 71. when the country code is present, 03 drops the 0 and becomes 3 while 70 and 71 are as is with or without country code.
123456: the phone number, always 6 digits.
here are the formats i'm trying to validate:
961 3 123456
961 70 123456
961 71 123456
03 123456
70 123456
71 123456
the spaces are here just for the sake of clarity, i am validating after stripping all spaces and non alphanumeric characters.
that's it, would be great if someone can help. thanks
I'm sure there's a slicker way to do it, but
^(961(3|70|71)|(03|70|71))\d{6}$
seems to work, assuming I understand the requirements.
^((961)?(7(0|1))|(961|0)3)[0-9]{6}$
It's a composition of these three regexes:
(961)?(7(0|1)) // 70 and 71 prefixes
(961|0)3 // (0)3 prefix
[0-9]{6} // main number
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With