views:

783

answers:

5

I am doing web development with Perl. I need to do phone number validation for all countries. Is there open source Perl module that can do the following?

For example: country = Malaysia, if user input phone number = +60127008007, after validate, it return this is a valid mobile number in Malaysia, where: In local Malaysia, we call: 0127008007 directly. "+6" is the international code.

Whereas if user input phone number = +600127008007, it returns as invalid phone number. Because locally in Malaysia, there is no such number as: 00127008007.

Any free module than can do this, in CPAN or otherwise?

A: 

It's practically impossible to keep an up-to-date list of valid area codes and number lengths for each country.

I suggest you check for this:

  1. An international calling code (1 to 3 digits)
  2. An area code (usually 3 digits, but ranges from 1 to 5 digits)
  3. A phone number (usually 6 to 8 digits, but ranges from 4 to 13 digits)

So you have between 6 and 21 digits, with special thanks to Austria. =)

That being said, you can never be sure a phone number is valid unless you call the number. You will have people working their way around the validation (eg. by entering a valid, public number that doesn't belong to them), so you should ask yourself: "is this really necessary?"

Can Berk Güder
A: 

I don't know Perl but that's semme to be a perfect case to use Regexp.

But anyway you will need one for each country, and obviously this can't include any country in the world but you can find phone patterns here.

MarmouCorp
+9  A: 

Have a look at Number::Phone::NANP (lNumber::Phone::NANP). I guess you will add new sub classes for uncovered countries.

weismat
+6  A: 

Validation assumes you know more than the person entering the data, but in this case you do not. Let's say that I am an American working a long term contract in Canada. I have kept my cell phone because the company I am working for is paying the roaming charges. When I visit your site and enter my American cell phone number and Canadian address your validation script claims it is invalid when it is not. This leads to customer confusion and anger.

If a valid phone number (or email for that matter) is an absolute requirement for your business you must call (or email) the person at the number (or email address) and have them enter a code you give them. There is hardware/software designed to automatically call numbers and provide a message to the recipient. You could use text to speech software like Festival to play a special key over the phone and then have them type it into your website to confirm that they have access to that phone. Even this is not infallible though, as I could buy a throw away cell phone or create a throw away email address.

Chas. Owens
+1  A: 

The point of validation is to help user by catching input errors of forcing them to use specific format during input.

There is a point where validation stops being helpful to the user. Usually, this is also the point where exceptions to the validation rules become too much for programmer to handle, since he entered the land of diminishing returns.

Why do you need validated phone number? Are you sure user won't try to enter two number in the same field and give up in frustration? Are you sure user won't enter something like "555-5555 (ask for Steve)" or "555-5555 (extension 123)" or some other totally valid input? Are you sure you're smart enough to predict every input user might need to enter in that field?

Don't try to outsmart your user. Help him. :)

Domchi