Reputation: 20001
I am using following regular expression to cover international phone numbers & some local phone number which can be in this format only
International Phone number
+123 456789123
+123456789123
+12 3456789123
+123456789123
Local Phone number format (Mobile no. followed by landlines numbers)
1234567890
123 4567890
123123456
12 3123456
Regular expression which i am using
^[\+]{0,1}[1-9]{1}[0-9]{7,11}$
This regular expression works well with international numbers only irrespective of prefix +
is added or not but doesn't allow any while space character.
I want it to support above formats for as show in example and should also support all international phone numbers
I am working on asp.net just in case if some one wants to know.
UPDATE:
I finally end up using following Regex which also handles extension
^([\+]?[0-9]{1,3}[\s.-][0-9]{1,12})([\s.-]?[0-9]{1,4}?)$
Upvotes: 2
Views: 10339
Reputation: 3109
Hi some comment about you're regex
[\+]{0,1} could be \+? // easier to read, + as optional
[1-9]{1} could be writen as [1-9]
[0-9]{7,11} should be [0-9\s.-]{7,11} // phone numbers can contain - or .
Your total regex would be
^\+?[1-9][0-9\s.-]{7,11}$
phone numbers could be written as
SECOND ATTEMPT
You could break your problems in 2 steps:
First match possible phone number by increasing range from 11 to 20
^\+?[1-9][0-9\s.-]{7,20}$
next step is to remove non numbers and verify length is between 8 and 12
string phone = "070.3233123";
string onlyNumbers= new String(phone.ToCharArray().Where(c => Char.IsDigit(c)).ToArray());
if (onlyNumbers.length > 8 && onlyNumbers.length < 12)
{
// we got a winner
}
Upvotes: 5
Reputation: 11
Below pattern will validate the following format
^\s*\+?[0-9]\d?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$
Results
+1 (281) 388 0388
+65 281 388-0388
+91 281 388 0388
+65 2813880388
+652813880388
+65(281)3880388
+65281388-0388
+1281388-0388
Upvotes: 0