Phil Young
Phil Young

Reputation: 1364

Valid UK telephone regex with extensions

I need a regex for php that allows 0-9, the string ext, spaces and + (for UK telephone numbers, ext is for extension). here is my attempt which doesn't work:

/^[\d -(ext)]+$/

Any ideas?

Upvotes: 0

Views: 560

Answers (2)

wesside
wesside

Reputation: 5740

Straight from Google...

Examples of accepted numbers:

02081234567

0208 123 4567

020 8123 4567

0208 123-4567

+44 208 123 4567

+44 (0) 208 123 4567

01234 567 890

+44 0 1234 567-890

07712 123 456

Examples of numbers that will not be accepted:

020812345678 123456789 07612 123 4567 +33 345 876 1298

/^(((44))( )?|((+44))( )?|(+44)( )?|(44)( )?)?((0)|((0)))?( )?(((1[0-9]{3})|(7[1-9]{1}[0-9]{2})|(20)( )?[7-8]{1})( )?([0-9]{3}[ -]?[0-9]{3})|(2[0-9]{2}( )?[0-9]{3}[ -]?[0-9]{4}))$/

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336468

You need to take the string out of the character class:

/^([\d -]+|ext)+$/

To allow ext just once:

/^[\d -]+(ext)?[\d -]+$/

Upvotes: 1

Related Questions