-1

I am having trouble find regex validation patterns for phone numbers in different countries and have little time to try and write my own and was hoping a regex guru would be able to help.

I've checked the usual sources like regexlib already, so if anyone can help i'd be grateful with any of them

I need a separate phone number validation expression for each of the following:

  • Germany
  • US
  • Australia
  • New Zealand
  • Canada
  • Asia
  • France
Scott Reed
  • 501
  • 1
  • 5
  • 18
  • What are the patterns and where are you stuck with them, something specific would be good – V4Vendetta Oct 24 '11 at 11:07
  • @Ranhiru, he could also do it in multiple steps, or perhaps store it in an array on on file, and iterate through them. – foxy Oct 24 '11 at 11:08
  • Are you sure you want to limit in this way? Do you cater for people with longer numbers, international numbers, extension numbers, etc? – Kieren Johnstone Oct 24 '11 at 11:10
  • possible duplicate of [A comprehensive regex for phone number validation](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation) – CodeCaster Oct 24 '11 at 11:13
  • Just thank your lucky stars the UK isnt in your list... haha good luck writing a regex *just* for the UK. – Jamiec Oct 24 '11 at 11:16
  • We're already sorted for UK validation as we have a validation expression that's tried and tested. Ideally I'm looking for as comprehensive validation as possible taking in to account international, regional, mobile phone. I only need a separate one for each country. Thanks – Scott Reed Oct 24 '11 at 11:47

4 Answers4

1

The format is here.

Writing the regex is not trivial, but if you specify the rules, would not be difficult.

Aliostad
  • 80,612
  • 21
  • 160
  • 208
1

Instead of making an elaborate regular expression to match how you think your visitor will input their phone number, try a different approach. Take the phone number input and strip out the symbols. Then the regular expression can be simple and just check for 10 numeric digits (US number, for example). Then if you need to save the phone number in a consistent format, build that format off of the 10 digits.

This example validates U.S. phone numbers by looking for 10 numeric digits.

protected bool IsValidPhone(string strPhoneInput)
{
// Remove symbols (dash, space and parentheses, etc.)
string strPhone = Regex.Replace(strPhoneInput, @”[- ()\*\!]“, String.Empty);

// Check for exactly 10 numbers left over
Regex regTenDigits = new Regex(@”^\d{10}$”); 
Match matTenDigits = regTenDigits.Match(strPhone);

return matTenDigits.Success;
}
Alon Adler
  • 3,984
  • 4
  • 32
  • 44
1

Phone number is a number, what you want to validate there ?

Here you can see how different numbers look like.

And there is no such country like Asia, this is a mainland with several countries.

  • Yes, I know Asia isn't a country, any expression would have to be as flexible as possible to cater for variations that exist in Asia. Phone numbers have lots of different formats, the validation routines are to make sure people have entered valid formats, not just numbers. – Scott Reed Oct 24 '11 at 11:50
1

It's close to impossible to get a single regular expression that will cover all countries.

I'd go with [0-9+][0-9() ]* -- this simply allows any digit to start (or the "+" character), then any combination of digits or parentheses or spaces.


In general validation any further is not really going to be of much use. If the user of the page wants to be contacted by phone, they'll enter a valid phone number -- if not, then they won't.

A better way to enforce a correct phone number and eliminate most simple miskeying is to require the number to be entered twice -- then the user is likely to at least check it!

Jeremy McGee
  • 24,842
  • 10
  • 63
  • 95
  • Or curse and hit Ctrl-C Ctrl-V. – CodesInChaos Oct 24 '11 at 11:17
  • @CodeInChaos - yes, but like duplicating emails, it's a minor speed-bump if it's important that it's right. – Jeremy McGee Oct 24 '11 at 11:27
  • I'm looking for just a list of regular expression for each of the counties, not one for all. I don't have the time to write them and the ones I've found were not of good quality so was hoping for ideas here rather than having to spend the time writing them – Scott Reed Oct 24 '11 at 11:41
  • My point is that you won't find one... better to leave this to the users to make sure they give you a way to get hold of them. – Jeremy McGee Oct 24 '11 at 11:50
  • Some of the areas being captured are double entered, some are validated and some are both. All of which are as defined in a signed off spec and a requirement relating to the CRM system our client uses. ergo they are necessary to be this way. – Scott Reed Oct 24 '11 at 11:56