Validating Domestic and International Phone numbers

Validation of both International and Domestic phone numbers presents it's own special challenge.  I came across a regex that handles this pretty well.

It matches these cases:

+42 555.123.4567
+1-(800)-123-4567
+7 555 1234567
+7(926)1234567
(926) 1234567
+79261234567
926 1234567
9261234567
1234567
123-4567
123-89-01
495 1234567
469 123 45 67
89261234567
8 (926) 1234567
926.123.4567
415-555-1234
650-555-2345
(416)555-3456
202 555 4567
4035555678
1 416 555 9292

While rejecting these:

926 3 4
8 800 600-APPLE

I located it here - would love to take credit but alas did not create it:
http://www.regexr.com/38pvb

And here is the Regex

^\s*(?:\+?(\d{1,3}))?([-. (]*(\d{3})[-. )]*)?((\d{3})[-. ]*(\d{2,4})(?:[-.x ]*(\d+))?)\s*$

Or as a Data Annotation

[RegularExpression(@"^\s*(?:\+?(\d{1,3}))?([-. (]*(\d{3})[-. )]*)?((\d{3})[-. ]*(\d{2,4})(?:[-.x ]*(\d+))?)\s*$",ErrorMessage ="Please match a valid international or domestic phone format")]
      

Comments

Popular posts from this blog

Linq Exclude from separate list

Sorting Ascending and Descending

Linq Query Syntax vs Method Syntax