Validate USA / Canada Phone Number

Hi

im creating a form and need to validate that he phone number is actually a USA based or Canada base phone number.

Does anyone know of a function or a way to do this.

Thanks,
Chris

Hi

You should make use of Regular Expression.

http://weblogtoolscollection.com/regex/regex.php shows how to user regex with PHP

and this http://www.thunderstone.com/doc/rex.html has the pattern for validating us numbers.

Enjoy !

how detailed do you want to get?

at a minimum, get 10 digits, where the first isn’t a 0 or 1.

or scrounge http://www.nanpa.com/ for the rules of valid phone numbers.

and if you have money to burn and want to possibly turn people away, subscribe to a service like lexis/nexis or bridger/chex and verify the identity of your users electronically.

Hi,

Thanks for the link, i just tried it out thought and didn’t seem to work for usa numbers. Any ideas? Here is the code snippet:

function validPhoneNumber($phone){
	if (preg_match ("/[1-9][01][0-9]-?[0-9]{3}-=[0-9]{4}", $phone)) {
		return "Good Phone";
	}
	else {
		return "Bad Phone";
	}
}

function validPhoneNumber($phone){
    if (preg_match ("/[1-9][01][0-9]-?[0-9]{3}-=[0-9]{4}/", $phone)) {
        return "Good Phone";
    }
    else {
        return "Bad Phone";
    }
}

You forgot the (/) ending delimiter.

Hi Again,

I just tested it out and it didnt work.

I tested it with the following code

// Test Function: validPhoneNumber()
$phone = "516-555-5555";
echo "$phone Phone Type:".validPhoneNumber($phone);

i also tested it with just

$phone = "5165555555";

and still no luck.

Hey,

what if i do this:

go to this link: http://www.consumer.att.com/global/english/usa_codes.html
and get all the usa area codes and check the phone versus those numbers.

Or is that considered really inefficient coding?

Thanks

Is there an even easier way to do it.

Is it true that only USA and CANADA have the format?

So that way i only have to see if it has a 10 digit phone number to validate it?

Thanks

Hi

I use this javascript code humm, not sure with PHP but still u can use it within PHP. like

<?php
if isset($_POST[‘submit’]) // if the submit button of the form clicked
{ ?>
javascript function goes here.

<? }

function checkPhone (phonestr) {
reg = /[1]\d{2}-\d{3}-\d{4}$/;
if(!reg.test(phonestr))
{
return false;
}
else
{
return true;
}
}

to check the number in the format of 123-345-6789 and its working .

try this
/[2][01][0-9]-?[0-9]{3}-=[0-9]{4}$/ as your pattern.


  1. 0-9 ↩︎

  2. 1-9 ↩︎

no, its not true that only USA and Canada has format.

Every country has a similar format like

country code - area code - phone number
or
country code - cell number

Thanks Zee, thats what i wanted to know.
Because, even if you use that validation function for phone number,
it can be from another country other than Canada and USA.

I will go ahead and validate it be checking all USA area codes that I posted
in the link above.

In terms of the format of the form, I like the way the fido website has it.
http://fido.ca/portal/home/homepage.jsp?lang=en

That way, the only validation you need is to check if its all numbers instead of worrying of - and () in the phone number.


$phone = '1-555-444-3322';

$c = array('abc' => 2, 'def' => 3, 'ghi' => 4, 'jkl' => 5, 'mno' => 6,
    'pqrs' => 7, 'tuv' => 8, 'wxyz' => 9
);

$phone = str_replace(array_keys($c), array_values($c), $phone);
$phone = preg_replace('/\\D+/', '', $phone);

if (preg_match('/^1?(\\d{3})\\d{3}\\d{4}$/' $phone, $match)) {
    // Vaild Phone
    var_dump($match);
} else {
    // Not vaild
}

Clean up into a function:


function validPhoneNumber ($phone) {
    
    $reg = array ('abc' => 2, 'def' => 3, 'ghi' => 4, 'jkl' => 5, 'mno' => 6,
        'pqrs' => 7, 'tuv' => 8, 'wxyz' => 9, '/\\D+/' => '');
    $phone = preg_replace(array_keys($reg), array_values($reg), $phone);

    if (preg_match('/^1?(\\d{3})\\d{3}\\d{4}$/', $phone)) {
        return true;
    }

    return false;

}

Hi

Just wanted to post an update of what I did and some code in case someone needs it:

I basically created a database with all the USA and Canada Area Codes.
(If you want just USA codes take everything before Number 250)
So all i do to validate its a legit number is check the db for the area code and return true if its there and false if it isnt.

Also, any comments comments on this way of doing it will be appreciated.

INSERT INTO `areacodes`.`areacodes` (`areaCodes`)VALUES
('907'),
('205'),
('251'),
('256'),
('334'),
('479'),
('501'),
('870'),
('684'),
('480'),
('520'),
('602'),
('623'),
('928'),
('209'),
('213'),
('310'),
('424'),
('323'),
('408'),
('415'),
('510'),
('530'),
('559'),
('562'),
('619'),
('626'),
('650'),
('661'),
('707'),
('714'),
('760'),
('805'),
('818'),
('831'),
('858'),
('909'),
('916'),
('925'),
('949'),
('670'),
('303'),
('720'),
('719'),
('970'),
('203'),
('860'),
('202'),
('302'),
('239'),
('305'),
('786'),
('321'),
('352'),
('386'),
('407'),
('561'),
('727'),
('772'),
('813'),
('850'),
('863'),
('904'),
('941'),
('954'),
('754'),
('229'),
('404'),
('678'),
('470'),
('478'),
('770'),
('706'),
('912'),
('671'),
('808'),
('319'),
('515'),
('563'),
('641'),
('712'),
('208'),
('217'),
('309'),
('312'),
('618'),
('630'),
('708'),
('773'),
('815'),
('847'),
('224'),
('219'),
('260'),
('317'),
('574'),
('765'),
('812'),
('316'),
('620'),
('785'),
('913'),
('270'),
('502'),
('606'),
('859'),
('225'),
('318'),
('337'),
('504'),
('985'),
('413'),
('508'),
('774'),
('617'),
('857'),
('781'),
('339'),
('978'),
('351'),
('301'),
('240'),
('227'),
('410'),
('443'),
('667'),
('207'),
('231'),
('248'),
('947'),
('269'),
('313'),
('679'),
('517'),
('586'),
('616'),
('734'),
('810'),
('906'),
('989'),
('218'),
('320'),
('507'),
('612'),
('651'),
('763'),
('952'),
('314'),
('417'),
('573'),
('636'),
('660'),
('816'),
('228'),
('601'),
('662'),
('406'),
('252'),
('336'),
('704'),
('980'),
('828'),
('910'),
('919'),
('701'),
('308'),
('402'),
('603'),
('201'),
('551'),
('609'),
('732'),
('848'),
('856'),
('908'),
('973'),
('862'),
('505'),
('702'),
('775'),
('212'),
('646'),
('315'),
('516'),
('518'),
('585'),
('607'),
('631'),
('716'),
('718'),
('347'),
('845'),
('914'),
('917'),
('216'),
('330'),
('234'),
('419'),
('567'),
('440'),
('513'),
('614'),
('740'),
('937'),
('405'),
('580'),
('918'),
('503'),
('971'),
('541'),
('215'),
('267'),
('412'),
('878'),
('570'),
('610'),
('484'),
('717'),
('724'),
('814'),
('787'),
('939'),
('401'),
('803'),
('843'),
('864'),
('605'),
('423'),
('615'),
('731'),
('865'),
('901'),
('931'),
('210'),
('214'),
('972'),
('469'),
('254'),
('281'),
('325'),
('361'),
('409'),
('430'),
('432'),
('512'),
('682'),
('713'),
('832'),
('806'),
('817'),
('830'),
('903'),
('915'),
('936'),
('940'),
('956'),
('979'),
('435'),
('801'),
('385'),
('340'),
('276'),
('434'),
('540'),
('703'),
('571'),
('757'),
('804'),
('802'),
('206'),
('253'),
('360'),
('425'),
('509'),
('564'),
('262'),
('414'),
('608'),
('715'),
('920'),
('304'),
('307'),
('250'),
('604'),
('778'),
('403'),
('780'),
('306'),
('204'),
('416'),
('647'),
('519'),
('613'),
('705'),
('807'),
('905'),
('289'),
('418'),
('450'),
('514'),
('438'),
('819'),
('506'),
('902'),
('709'),
('867'),
('600');