Reputation: 1055
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
Reputation: 59701
This regex should work for you:
$mobileNumber = "0905 222 222";
if ( preg_match("/^(\+?)([0-9] ?){9,20}$/", $mobileNumber) )
echo "matches!";
Upvotes: 1
Reputation: 174796
You could try the below regex,
^\+?\d{3} ?\d{3} ?\d{3} ?\d{3}$
^
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