Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am trying to find best way to validate a mobile number with in a country.

Currently my understanding is: User can enter whatever format they want in mobile numbers and its a waste of time and energy to validate it against a set of regular expressions.

My application is not a critical one like banking application and if the user is entering an invalid mobile number, it is at his own risk to get updates (like activate account/ do something with the application)

So I think the best way is to check for mobile number length and whether all are digits.

I want to know the best way forward and is there any good resource (non-scattered) to get all mobile number length validations based on a country code?

share|improve this question
6  
eh, mobile numbers (like phone numbers) are very country specific, your best bet would be sending a message for validation –  ratchet freak Jan 7 '13 at 12:05
1  
Checking that all are digits will fail if the user enters "+44123456789" as their number. –  ChrisF Jan 7 '13 at 12:08
5  
Check out libphonenumber: a library for handling phone numbers (made by Google, btw). –  Joachim Sauer Jan 7 '13 at 12:17
1  
Well then check for length and standard prefixes, and augment that with a validation text message. Seems like the simplest way to go. –  elssar Jan 7 '13 at 14:59
1  
@RohithNair - Check out the source view for the different languages that aren't available in the binary download section. It has both C++ and JavaScript source libraries that you can use, as well as information on how to use them. –  Shauna Jan 7 '13 at 15:23
show 5 more comments

1 Answer

Dealing with phone numbers it not trivial. Every country has their own specification.

Examples of variations (for each country):

  1. The area code length can vary
  2. The mobile phone area codes are different
  3. The native format is different

You could of course force the usage of E.164 (+<countrycode><areacode><number>) but that doesn't feel very user friendly. You can not just strip all white spaces and just add a + and think that it's E.164. For instance Swedish phone numbers should be stripped of a leading 0 in the area code when converting to E.164.

To make a long story short: It's pointless to validate mobile phone numbers by yourself.

Options:

  1. Send a SMS to the phone containing a code (two step validation)
  2. Use an existing phone number library
  3. Get the number plan for every country. (Swedens can be found here: http://www.pts.se/en-GB/Industry/Telephony/Numbering/National-numbering-and-addressing-plans/The-international-public-telecommunication-numbering-plan-E164/)
share|improve this answer
    
UK numbers need the leading 0 stripping and replacing by +44 (the country code) as well. I was going to suggest it was true worldwide, but I'm willing to bet that there are a few that don't follow that rule. –  ChrisF Jan 7 '13 at 12:35
    
@ChrisF: US numbers do not have a leading zero (if we can believe in movies ;)) –  jgauffin Jan 7 '13 at 12:36
    
He, he. True. Don't their codes start with a 1? So what would be an "0800" number in the UK would be a "1800" number in the US. –  ChrisF Jan 7 '13 at 12:37
1  
No. 1 is their country code, as 44 is the one for UK. –  jgauffin Jan 7 '13 at 12:42
    
@ChrisF: The leading 0 in UK numbers (and those of many other countries) is a local convention called a trunk prefix that's only used inside the country. The North American Numbering Plan's trunk prefix is 1, which can cause some confusion because the country code for that dialing plan is also 1. So just as you don't dial +1 1 202 456 1111 to reach the White House from London, you don't dial +44 0 20 7930 4832 to reach Buckingham Palace from Washington. Once all NANP areas have switched to ten-digit dialing, the trunk prefix in the US can die off with no ill effects. –  Blrfl Jan 7 '13 at 13:09
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.