23

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
  • 1
  • 13
Ranger
  • 993
  • 1
  • 5
  • 12
  • 1
    What about phone numbers expressed with letters, e.g., 0800DIALTHIS? – mouviciel May 09 '11 at 09:37
  • 3
    What are you trying to prevent? The user lying? The user not wanting you to know their number? The user being too lazy to enter anything? The user doing a typo? – Benjol May 09 '11 at 12:25
  • @ mouviviel : yes that also i need to think about i am taking one valdation scenario at a time so i will think about it. – Ranger May 10 '11 at 05:44
  • @Benjol : This application is a commercial one so we can't risk on our coustomers i know a user will enter a correct number but what if he/She does not. Valdation are pretty important as they save application not only typos. I don't want my DB to filled with useless junk. – Ranger May 10 '11 at 05:47
  • 5
    well what are you doing to validate their email addresses? You'll observe that nearly all websites do "real life" validation (i.e. sending you a mail). I think that if the phone number is *that* important, you'd need to do something equivalent. – Benjol May 10 '11 at 07:05
  • @mouviciel There is an actual number backing that, they can enter that. – EpicKip Dec 14 '17 at 15:45

6 Answers6

27

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,427
  • 35
  • 50
  • 7
    +1 for "users just wanted to convey you an important piece of information". I've too often seen unnecessary validation that simply annoys the user without any actual technical (or organizatorical) requirements. – Christian Seifert May 09 '11 at 07:06
  • 6
    +1 - phone number validation (especially defined this broadly) is often pointless, not future-proof (KL5-6743 used to be a valid phone number, for example), and just annoys users who have special cases. Make you phone numbers a big text field and let the automated dialers (if there are any in this scenario) parse what they can. The exceptions can be raised to humans who can handle the situation appropriately. – E.Z. Hart May 09 '11 at 07:22
  • 8
    +1 and **most importantly** just because a phone number is valid, it doesn't mean it exists, and even if it exists, it doesn't mean it belongs to the person in question... – Benjol May 09 '11 at 12:23
  • Oh yeah, i will figure out the countries where our major users live and then i will write some validation code. Thanks All, i appriciate your efforts. – Ranger May 10 '11 at 05:50
  • 6
    @Ranger That is the exact opposite of what the answer was getting at. – Sebastian Redl Dec 14 '17 at 10:12
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.

Arseni Mourzenko
  • 134,780
  • 31
  • 343
  • 513
  • 1
    It would help to show the use of the brackets in your examples. International numbers often use them to enclose the leading zero of the area code to indicate that you do not dial it when calling from abroad. E.g. `+31 (0)30 1234567` where 31 is the Netherlands, 030 is the area code and 1234567 is the subscriber. – Marjan Venema May 09 '11 at 05:51
  • 2
    Don't forget an optional field for extension code (which might need to allow the pound or asterisk key). – rwong May 09 '11 at 09:59
  • @MarjanVenema, I don't do much international dialing, but it's standard to add the zero, and add brackets (0) to tell you NOT to dial it? Wouldn't it be simpler to just leave out the 0 altogether? – CaffGeek Feb 03 '12 at 18:24
  • @Chad: Nope. It tells international users they need to leave it out and tells national users they need to use it. Not all countries have area codes starting with a zero. Doing it this way ensures that when somebody sees the number they know what to do when they are in and when they are out of the country. Not everyone calling nationally needs to be a resident after all... – Marjan Venema Feb 03 '12 at 18:42
  • @MarjanVenema, oh, I see, didn't realize national users needed it, learned something new today. – CaffGeek Feb 03 '12 at 19:58
  • In Russia it is typical to write numbers as +7 (123) 456-78-90 or even +7 (123 4) 56-78-90. The brackets denote the region prefix, which could start with zero until 2005. Within a prefix you can dial just the last 6-7 digits, and to call another prefix you use +7 (internationall format) or 8 (national), and that 8 is going to be replaced with a 0 by 2025. Also companies often have additional suffixes after a pause (often written as "добавочный 1234" after the number) to address all their phones while using just one external number, and the same suffixes are used as short internal numbers too. – EvgEnZh Nov 20 '21 at 09:43
  • So I guess the meaning of the brackets is opposite to what was described in previous comments, it tells [inter]national users to dial it while locals can skip it. – EvgEnZh Nov 20 '21 at 09:50
4

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.

Andy Canfield
  • 2,083
  • 12
  • 10
  • 5
    +100. There's nothing worse than a computer saying "please remove the - from the number", as if the computer has no clue how to do that on its own. Same for credit card numbers, FWIW. – Bryan Oakley Feb 03 '12 at 17:36
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
  • 16,695
  • 3
  • 33
  • 81
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
  • 2
  • 15
  • 1
    I don't agree to make sure no letters are accepted -- go ahead and accept the, then just ignore them. People naturally write extensions with a leading "x" (eg: 555-1212 x123). Why not allow that? Humans can parse it just fine, and if the computer needs to use it, it can strip out the non-digits trivially. – Bryan Oakley Feb 03 '12 at 17:35