Telephone Validation Method






4.50/5 (2 votes)
Validate phone number according to the North American Numbering Plan
Introduction
The function provided below validates a telephone against the requirements of the North American Number Plan (NANP) as administered by Neustar, Inc.
Background
While designing a database application for the company I was working for at the time, I needed a way to ensure that the telephone numbers entered were valid. Initially, I used just regular expressions to make sure that the numbers were valid, but in time it was discovered that some of the phone numbers entered matched the valid pattern, however the numbers were still invalid. The North American Number Plan specifies rules that a phone number must follow such as what range of numbers are valid for area codes, exchange codes, test codes, etc.
Using the Code
This function will accept either a 10-digit or 11-digit phone number, with or without hyphens. Any hyphens are stripped out and any leading '1' (if present) is removed, leaving a 10-digit phone number which is validated. A 10-digit phone number is made up of the following parts:
NPA-NXX-XXXX
Where:
NPA = Number Plan Area Code (Area Code) - 3 digits
NXX = Central Office (Exchange) Code - 3 digits
XXXX = Subscriber Number - 4 digits
The code is highly commented along with the name, and ID numbers, of the reference documents used to develop this function. I believe it meets just about, if not every, requirement for phone numbers complying with the NANP. If there are any requirements missing, I apologize in advance; there are many industry technical documents on this topic and some of it is beyond my understanding.
'IsTelephoneNumberValid validates a phone number against the North American Numbering Plan
'(NANP) as administered by Neustar, Inc.
'and the requirements of the Industry Numbering Committee (INC)
'
'NANP requirements gathered from the following resources:
'--------------------------------------------------------
'ATIS-0300048: 555 NXX Assignment Guidelines (Issued: September 30, 2011)
'ATIS-0300051: Central Office Code (NXX) Assignment Guidelines (Issued: August 3, 2012)
'ATIS-0300055: NPA Allocation Plan & Guidelines (Issued: September 30, 2011)
'NANPA Number Resources - NPA (Area) Codes (http://www.nanpa.com/area_code) accessed on
'September 29, 2012
''' <summary>
''' Returns a Boolean value indicating whether the specified number is a valid phone number
''' in accordance with the North American Numbering Plan (NANP).
''' </summary>
''' <param name="number">Phone number to validate. Phone number must a 10-digit number
''' with, or without, a leading '1' and with, or without, separator characters.</param>
''' <returns>Returns a Boolean value indicating whether the specified number is a valid
''' phone number in accordance with the North American Number Plan (NANP).</returns>
Public Function IsTelephoneNumberValid(number As String) As Boolean
Const NRegEx As String = "^[2-9]\d{2}$" 'Number Plan Area Code and Exchange Code Pattern
Const XXXXRegEx As String = "^\d{4}$" 'Subscriber Number Pattern
Dim strippedNumber As String 'Phone number with only numbers
Dim NPA As String 'Numbering Plan Area Code (Area Code)
Dim NXX As String 'Central Office (Exchange) Code
Dim XXXX As String 'Subscriber Number
'Remove anything that is not a number from the phone number (e.g. hyphens)
'This allows the calling function to not have to format the number for this function
strippedNumber = Regex.Replace(number, "[^0-9]", String.Empty)
'Check if there is a leading '1' in the phone number
If strippedNumber.Length = 11 AndAlso strippedNumber.Substring(0, 1) = "1" Then
'Remove the leading '1' from the stripped phone number
strippedNumber = strippedNumber.Substring(1, 10)
ElseIf strippedNumber.Length <> 10 Then
'If the phone number doesn't have a leading one
'then it must be 10-digits long to be a valid phone number.
Return False
End If
'Get the Number Plan Area Code (Area Code) from the
'phone number (first 3-digits of phone number)
NPA = strippedNumber.Substring(0, 3)
'Get the Exchange Code from the phone number (middle 3-digits of phone number)
NXX = strippedNumber.Substring(3, 3)
'Get the Subscriber Number from the phone number (last 4-digits of phone number)
XXXX = strippedNumber.Substring(6, 4)
'Check if the Number Plan Area Code, Exchange Code,
'and Subscriber Number match the appropriate patterns
If Not Regex.IsMatch(NPA, NRegEx) OrElse Not Regex.IsMatch(NXX, NRegEx) _
OrElse Not Regex.IsMatch(XXXX, XXXXRegEx) Then
'One of the fields do not match; invalid phone number
Return False
ElseIf NPA.Substring(0, 2) = "37" OrElse NPA.Substring(0, 2) = "96" Then
'Numbering Plan Area Codes (Area Code) beginning with 37 or 96 (Area Codes 37X and 96X)
'are reserved by the Industry Numbering Committee (INC) for unanticipated future purposes
Return False
ElseIf NXX.Substring(1, 2) = "11" AndAlso (CInt(NXX) >= 211 AndAlso CInt(NXX) <= 911) Then
'If the Exchange Code is 211, 311, 411, 511, 611, 711, 811, or 911, then the Exchange Code is invalid
Return False
ElseIf NXX = "912" OrElse NXX = "913" OrElse NXX = "914" _
OrElse NXX = "915" OrElse NXX = "916" Then
'Exchange Codes 911-916 are invalid Exchange Codes
'because they are too similar to 911 (emergency services)
Return False
ElseIf NXX = "700" OrElse NXX = "950" OrElse NXX = "958" OrElse NXX = "959" Then
'Exchange Code 700: Used by customers to verify their intraLAPA PIC
'Exchange Code 950: Used by industry to access Feature Group B
' Carrier Identification Codes (CIC) and has special Automatic Message Accounting (AMA) triggers
'Exchange Code 958 & 959: Used by industry as standard test codes
Return False
ElseIf NXX = "555" Then
'Check if Exchange Code is '555'
If CInt(XXXX) >= 100 AndAlso CInt(XXXX) <= 199 Then
'If Exchange Code is '555' and Subscriber Number is
'between '0100' and '0199' (inclusive) then phone number is invalid
Return False
End If
End If
'If we have reached this point the phone number is valid
Return True
End Function
History
- 09/12/2014 - Updated an error which would cause a valid exchange code of 111 to be returned as invalid. Thanks to Jeremy Forsythe for finding this and pointing it out. Fixed a couple of small grammar errors.
- 10/18/2012 - Updated an error which would allow a phone number like 718-377-11205 to be entered and recognized as valid. Thanks to Matt Heffron for finding this and pointing it out. Fixed some grammar errors.
- 10/16/2012 - Initial version