My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
PhonesClass  
NANPA Phone Validation class.
PHPClass, Phones, Featured
Updated Jan 23, 2010 by cdburg...@gmail.com

phones

phone - phone validation according to NANPA specifications

Description

Function

bool validatePhone(string $phone [, mixed $filter])

validate the phone number with an optional filter to remove specific types of phone numbers.

Function

formatPhone(string &$phone [, string $delimiter [, bool $country_code]])

format the phone number. delimiter defaults to hyphen (-) and country code defaults to FALSE.

Parameters

phone

The phone number to be acted upon
filter
Specific name of a filter to make sure the phone number does not match. Allowed options are toll_free, toll, toll_all, service, carrier, special, or all. This can be passed as a string or an array of filters.
delimiter
The character you want to use between the NPA, NXX, and XXXX of the phone number.
country code
Set to TRUE to include the 1 on the front of the phone number, otherwise set to FALSE.

Return Values

validatePhone

Returns TRUE if the phone number is valid, FALSE otherwise.

formatPhone

Sets the class variables phone_number and extension.

Examples

Example 1: Validate a phone number

<?php

include('phones.php');

$validate = new Phones;

$phone = '+1 285 555 1213 ext 27';

$phone_number = '+1 258 555 1213 ext 27';

if(list($phone, $ext) = $validate->validatePhone($phone_number, array('toll', 'special'))){
	echo $phone.' is VALID';
} else {
	echo $phone.' is INVALID';
}

?>

Example 2: Validate phone number filtering for toll and special numbers

<?php

include('phones.php');

$validate = new Phones;

$phone_number = '+1 258 555 1213 ext 27';

if(list($phone, $ext) = $validate->validatePhone($phone_number)){
	echo $phone.' is VALID';
} else {
	echo $phone.' is INVALID';
}

?>

Exmaple 3: Format a phone number using a period

<?php

include('phones.php');

$validate = new Phones;

$phone_number = '+1 258 555 1213 ext 27';

list($phone, $ext) = $validate->formatPhone($phone_number, '.');
echo $phone.' ext.'.$ext;

?>

About

The North American Numbering Plan (NANPA) defines how phone numbers are assigned. Based on the rules assigned by NANPA, I have created a class that will validate any NANPA specific phone number. Note that this also includes Canada as they are part of NANPA. I have also included some basic filtering methods that will check for specific types of numbers as well.

VALID : any phone number that passes the NANPA requirements that is assignable now or in the future (i.e. 555-1212 is pre-assigned as Information similar to 411). This does not exclude area codes or exchanges that may not be in use.

The Phones class will validate any North American Numbering Plan phone number as outlined by the governing body NANPA. The purpose of this class is to validate whether or not the phone number in question is assignable. There are also methods that will allow for specific types of filtering.

Summary

The validation routine will by default check for all of the following. There is one acception.

Reference Information

NPA (Area) Codes

  • N is any digit 2 thru 9 (area code cannot start with a 1)
  • P is any digit 0-8
  • N11 service codes not used as area codes (i.e. 911, 811, 711)
  • N9A expansion codes cannot be used (known as expansion codes)
  • 37A & 96A cannot be used (set aside for unanticipated purposes)
  • PA that match known as ERC (easily recognizable codes) [ie. 888, 800, 555]
Note: ERCs (Easily Recognizable Code) will pass validation unless filtered on special.

Central Office Codes (exchanges, prefixes, or simply NXXs)

  • N any number 2-9
  • N11 service codes not used as area codes (i.e. 911, 811, 711)

Carrier Identification Codes (CIC) XXXX

  • X is any digit 2 thru 9

NXX-XXXX Combinations (Phone Number)

  • cannot be 555-0100 thru 555-0199 (fictional numbers)
  • 555-1212 is for information (not assignable)

Summary of Cleansing

Phone numbers are written using many different delimiters and many combination's thereof. After looking into the many different approaches available for validating a phone number with all of these various combination's, the task began to become not only burdensome, but incomplete. While it is possible to account for the majority of common formats, I found that on occasion a format would pop up that was not accounted for and the valid number it contained would be flagged as invalid.

This prompted me to write a simple but effective method for cleansing a phone number. This method will check for an extension and separate the phone number based on that extension. It will then remove all non-digit characters. This effectively should leave an array of 1) a 10d or 1+10d phone number and 2) an optional extension which can then be verified. If there was no extension, then the section variable in the array will be empty.

Summary of Filters

This class will validate a phone number. However, there are business rules that may require that certain numbers be considered invalid. These numbers should also be flagged. For example, if the business requirements indicate that the phone number must be residential and toll free numbers are not likely to appear as a phone number for a residence, then toll free numbers should be flagged as invalid. I have established the following filters with their accompanying names:

  • TOLL_FREE - Toll Free Numbers where NPA matches one of 800, 822, 833, 844, 855, 866, 877, 880 through 882, 888
  • SPECIAL - Any area code ending in two digits that are the same
    • TOLL - Premium Toll Numbers where NPA matches 900
    • TOLL_ALL - combination of all toll numbers (free & premium)
    • SERVICE - personal communication services where NPA matches 500
    • CARRIER - Long Distance carrier where NPA matches 700 (not widely used anymore)
  • ALL - Where NPA matches any of the pre-defined filters listed here

Summary of Format Phone

Once the phone is validated and filtered, you may want to format it with delimiters to make it easier to read. I have created this method to use the most common format in North America which is (in my opinion): NPA-NXX-XXXX or 1-NPA-NXX-XXXX However, I realize that some people prefer to have their phone numbers formatted using different delimiters, so I made the delimiter a variable. All you do is pass the method the phone and the delimiter and the method will return the formatted phone number. If you only send the phone number, it will use - as default.

I have also created the ability to add or remove the Country Code which is the number one (1) to all numbers. The third option you can pass to the method is the bool tue or false. If true, the number will come back as 1-NPA-NXX-XXXX. If false, then the 1- will be removed.


Sign in to add a comment
Powered by Google Project Hosting