How to match phone numbers?

Phone numbers are not an ideal candidate for regular expressions. You are best off looking at libphonenumber and libphonenumber-for-php to do phone number validation, along with sending a confirmation text message if it absolutely needs to be valid. That being said, there are some usecases for regular expression matching… So here you are.

10 Digit North American Number

This will match a standard North American phone number and also validate that the area code and second three digits are legal.

Example: 201-867-5309

Regex: ^\(?([2-9][0-8][0-9])\)?[-. ]?([2-9][0-9]{2})[-. ]?([0-9]{4})$

10 Digit North American Number with +1

This will match a standard North American phone number and also validate that the area code and second three digits are legal. It also allows an optional leading +1.

Example: 201-867-5309

Regex: ^(?:\+?1[-. ]?)?\(?([2-9][0-8][0-9])\)?[-. ]?([2-9][0-9]{2})[-. ]?([0-9]{4})$



×

Subscribe

The latest tutorials sent straight to your inbox.