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?
10
Dealing with phone numbers it not trivial. Every country has their own specification.
Examples of variations (for each country):
- The area code length can vary
- The mobile phone area codes are different
- 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:
- Send a SMS to the phone containing a code (two step validation)
- Use an existing phone number library
- 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/)
8