Tuesday, November 8, 2011

Easy Phone Number Checks


So this is a simple free example of how to check a phone number via a mysql database. Yes more complete validation options are available, some even have to pay for, but this is better than no check at all. 

The data is available here : wget -q http://www.telcodata.us/custom/telcodata.dump

It loads a table like this:
CREATE TABLE `telcodata` (
`npa` int(3) NOT NULL,
`exchange` int(3) NOT NULL,
`thousands` int(3) NOT NULL DEFAULT '-1',
`company` text,
`ratecenter` text,
`clli` text,
`type` text,
`emaildomain` text,
`parentco` text,
`state` text,
`zip` text,
`lat` float(9,5) DEFAULT NULL,
`lon` float(9,5) DEFAULT NULL,
`latanum` int(11) DEFAULT NULL,
`ocn` varchar(4) NOT NULL,
`ilec` varchar(4) DEFAULT NULL,
`ilecname` text,
`tandem` text,
KEY `rcstate` (`ratecenter`(11),`state`(2)),
KEY `npaexch` (`npa`,`exchange`,`thousands`)
)

Once the data is loaded you can use this with data you have gathered. This could be from websites or data warehouse data who knows...

Then you can check to see what type of a phone number it is ?

SELECT npa,exchange,thousands, company , ratecenter , type, parentco, CONCAT(npa,exchange,'0000','@',emaildomain) as send_a_txt
FROM telcodata where npa=303 and exchange=335 and ( thousands=8 or thousands < 0 )  ORDER BY thousands desc LIMIT 1 \G

npa: 303
exchange: 335
thousands: 8
company: CELLCO PARTNERSHIP DBA VERIZON WIRELESS - CO
ratecenter: DENVER
type: WIRELESS
parentco: Verizon
send_a_txt: 3033350000@vtext.com
1 row in set (0.00 sec) 

I have placed 0000 in place of the thousands field, if you wanted a valid thousands option you would replace it with your data.

A none cell looks like this 

 SELECT npa,exchange,thousands, company , ratecenter , type, parentco ,  CONCAT(npa,exchange,'0000','@',emaildomain) as send_a_txt FROM telcodata where npa=303 and exchange=623 and ( thousands=9 or thousands < 0 ) \G
*************************** 1. row ***************************
       npa: 303
  exchange: 623
 thousands: -1
   company: QWEST CORPORATION
ratecenter: DENVER
      type: RBOC
  parentco: NULL
send_a_txt: NULL
1 row in set (0.05 sec)


In this case you can check to see if the data given is a cell phone or not by checking the type field. Your tests are only as good as your data but this is a start.