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.

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?

share|improve this question
1  
What about phone numbers expressed with letters, e.g., 0800DIALTHIS? –  mouviciel May 9 '11 at 9: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 9 '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 5: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 5:47
4  
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 7:05
add comment

5 Answers

up vote 16 down vote accepted

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.

share|improve this answer
6  
+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. –  perdian May 9 '11 at 7: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 9 '11 at 7:22
7  
+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 9 '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 5:50
add comment

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.

share|improve this answer
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 9 '11 at 5:51
2  
Don't forget an optional field for extension code (which might need to allow the pound or asterisk key). –  rwong May 9 '11 at 9: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 3 '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 3 '12 at 18:42
    
@MarjanVenema, oh, I see, didn't realize national users needed it, learned something new today. –  CaffGeek Feb 3 '12 at 19:58
add comment

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.

share|improve this answer
1  
+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 3 '12 at 17:36
add comment

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.)

share|improve this answer
3  
You are forgetting the \+ sign for international access –  Marjan Venema May 9 '11 at 5:47
2  
allow the letter x, too -- at least in the US it's common to prefix an extension with an x (eg: 555-1212 x123). –  Bryan Oakley Feb 3 '12 at 17:37
add comment

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.

share|improve this answer
    
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 3 '12 at 17:35
add comment

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.