Michal Takáč
Michal Takáč

Reputation: 1055

Regex for phone number in international format

I need to write a regex that will match only this formats

+420 000 000 000
+420000000000
420 000 000 000
420000000000

It can't match any a-z character in any part of string, just numbers, white space and "+" on the beginning.

Upvotes: 1

Views: 7558

Answers (2)

Rizier123
Rizier123

Reputation: 59701

This regex should work for you:

$mobileNumber = "0905 222 222";

 if ( preg_match("/^(\+?)([0-9] ?){9,20}$/", $mobileNumber) )
            echo "matches!";

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174796

You could try the below regex,

^\+?\d{3} ?\d{3} ?\d{3} ?\d{3}$

DEMO

  • ^ Asserts that we are at the start.
  • \+? optional +
  • \d{3} Matches exactly three digits.
  • <space>? optional space
  • $ Asserts that we are at the end.

Upvotes: 7

Related Questions