Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate phone number with a plus (+) sign and "(0)" by regex? [duplicate]

Tags:

c#

regex

I see a lot of solutions to validate a phone number by Regex... But I want some characters in my phone number such as +31(0)12 for example.

If you have string with the value +31(0)12-1234567. This is a correct notation of a phone number. How could you validate by using Regex?

I have tried this:

Regex phonePattern = new Regex(@"\+\d{3}?(\0)\d*\-\d*"); 

What do I wrong?

Other examples are: +31(0)6-12345678 or +31(0)123-123456

like image 310
user1531040 Avatar asked Dec 11 '25 07:12

user1531040


1 Answers

I had this issue too and found no duplicate here on SO to Regex a number with a plus + sign being a phone number.

Finally I formatted this Regex myself:

Regex phonePattern = new Regex(@"\s*(?:\+?(\d{1,3}))?([-. (]*(\d{3})[-. )]*)?((\d{3})[-. ]*(\d{2,4})(?:[-.x ]*(\d+))?)\s*");

See it here on regex 101

This will match:

+nnnnnnnnnn
+nnn-nnn-nn
nnnnnnnnnnn
nnnn-nnnn-n
(nnn)nnnnnn
+(nnn)nnnnn
like image 172
Koby Douek Avatar answered Dec 13 '25 19:12

Koby Douek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!