24

So I am designing a webpage which will be used for global users, includes users from Canada, US, India, UK etc. I need to apply validations for this phone number field, but I'm not sure the best way to go about this.

Some of the valid formats I can think of are:

  1. 1800123456 (India)
  2. use of "-" in US phone numbers

I am a bit confused about what special characters I should allow a user to enter (eg.- / ()). How have others solved this in the past?

cdeszaq
  • 322
Ranger
  • 1,003

6 Answers6

28

Just because you can constrain something (that's what you're paid for, as a programmer, to write code), that doesn't mean you should actually do this.

What's the point of validating a number? Why is it useful? Will it fail if a user enters "0800DIALTHIS", or "私は電話番号を持っていない", or "(499) 123-45-67 добавочный 4425"? It surely will (you say "global". don't you?), while users just wanted to convey you an important piece of information on how to contact them via phone.

Besides, they way data are stored should be governed by the way they're used. How will you use the numbers? Will they be used to auto-send SMS spam—or they'll be manually dialed by managers? If it's the latter, then allowing any additional characters is okay, since human brain will be able to parse them the way they see the most appropriate; and in this case you don't really need any validations, which will just annoy human users.

P Shved
  • 7,477
  • 36
  • 51
7

It would be very difficult to know if a number is valid or not, since different countries have different formats. In France, for example, 06 12 34 56 78 is a valid phone number, whereas 00 12 34 56 78 is not, since the number can start only by 01 to 09, 06 and 07 being mobile phones.

Also, the phone numbers can be written in several formats even in the same country. I've seen:

  • 06 12 34 56 78
  • 0612345678
  • 06.12.34.56.78
  • 06-12-34-56-78
  • +33612345678
  • +33 (0)6 12 34 56 78

the last two being the international form.

In general:

  1. Allow brackets, + symbol only as a leading character, dashes, dots, commas and spaces.
  2. If possible, remember only the normalized form (i.e. +33(0)612345678).
  3. If this is not possible, at least remove separator characters (like spaces).

Going too deeply into validation per countries is not recommended. Not only there is a large amount of rules, but also rules change. For example in France, few years ago there were no phone numbers starting by 07 or 09. With the increasing number of mobile phones appeared 07. With VoIP services appeared 09.

You may also take in account special numbers. For example 3635 is a valid number in France, even if it contains only four digits and starts with a non-zero.

5

DO not restrict the characters; take everything he gives you. But when you dial it, take only the digits. Even then it might work wrong. I was in Thailand a year before I learned that 123-4567-9 meant not an extension, but a range of numbers, 1234567 through 1234569. When I dialed "12345679" it worked, because the phone company ignored the extra digit. Take anything, dial only digits.

I love it that my new phone allows spaces in the phone number. I can enter "123 456 7890" just like that, not all mashed together.

2

Google has lib for this:

Google's common Java, C++ and JavaScript library for parsing, formatting, and validating international phone numbers.

https://github.com/googlei18n/libphonenumber

It does best effort, I do think there must be cases that are not handled as this is by no means an easy task. Let users input numbers and letters, remove letters later with code.

0

Brackets, dash, and space.

Once the user has entered the phone number containing those symbols, you can validate them according to the country-specific rules (which you'll need to gather those rules for each country), and then the non-digits can actually be dropped when it is processed internally in your software.

This is because IDD (international direct dialing) makes it impossible to require any "special characters" to be actually used in dialing a number, with the exception of pauses in punching the digits.

(I'm not knowledgeable in this at all; please correct me if I'm wrong.)

rwong
  • 17,140
0

The only thing that should be validated is the number of digits - does it match what you would need to call?

Access codes can/should not be allowed to be entered by a user (i.e +1 for the US). I'll tell you why - if it is an American entering it, and they are not all that savvy, they will probably want to enter 001 instead of 1. Reason being that 00 is how you access an international number from within the US. They don't know what the difference is between that and just +1. If you're calling landlines between area codes (and sometimes in the same area code), you'll also enter a 1 before that number. Just like Japan.

Take Japan. Provincial land lines are accessed with a 0 on the front of them. But if you're dialling from outside of Japan, you don't need that 0.

Mobile numbers in China are 10 digits and in Hong Kong they are 8. If your form captures country, then don't even ask for country code at the beginning of the number - just auto-show it in read-only text right next to the number they need to enter.

The list goes on.

Just validate the number of digits (make sure no letters are accepted) and make it easy for an end-user to enter the right number of digits. I.e. they shouldn't have to enter country code and if your form is super smart, they may not even need to enter area code if it's a landline number. Or if they start to enter things like an access code, like 0 in front of a Japanese number, just let them know they don't need to.

Every other character, besides digits, should then be a moot point - they don't have a chance to enter them.

Todd Main
  • 439