Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Telephone Validation Method

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
18 Oct 2012CPOL2 min read 25.5K   3   5
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.

VB.NET
'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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Sci-Med Codimg
United States United States
I started programming around 10 years old. C/C++ was the first language I learned and then moved on to Assembly and finally VB/VB.NET which I have stuck with all these years. However, I have kept myself open to new languages and currently learning to work with Rust.

Besides computer programming, I have also learned microchip programming, starting with the Microchip PIC line and now using Raspberry Pi when it is convenient. I also have experience with HTML/ASP.NET/JavaScript/JQuery and built several websites for one of the largest environmental companies in NJ that I use to work for. These included their main website, and e-commerce site for the environmental products they sold, and a website for their training division which managed students and test grades. I wrote several large applications for them as well (e.g. a database front-end, a PDA application for data entry, and a program for analyzing data from an X-ray fluorescence machine and writing a report with recommendations based on the data).

Currently I work for a MSP that serves a good portion of the charter schools in NYC as well as a few small business and NPO's in NY, CT, and MA. Besides providing troubleshooting help, I develop applications which help streamline our work or our clients.

Comments and Discussions

 
GeneralMy vote of 5 Pin
jcxtsoftware2-Nov-20 4:04
jcxtsoftware2-Nov-20 4:04 
QuestionN11 area codes Pin
Member 1187227429-Jul-15 7:26
Member 1187227429-Jul-15 7:26 
QuestionAnother error..? Pin
Jeremy Forsythe7-Jul-14 8:34
Jeremy Forsythe7-Jul-14 8:34 
Shouldn't this:
VB
ElseIf NXX.Substring(1, 2) = "11" AndAlso (CInt(NXX) >= 211 OrElse CInt(NXX) <= 911) Then


be:
VB
ElseIf NXX.Substring(1, 2) = "11" AndAlso (CInt(NXX) >= 211 AndAlso CInt(NXX) <= 911) Then

??

if you pass NXX=111 it will fail the >=211 but pass the <= 911 so the OrElse will be true, but it should evaluate to false, so it should be AndAlso..

Great work on this, by the way.
AnswerRe: Another error..? Pin
Dominick Marciano12-Sep-14 6:47
professionalDominick Marciano12-Sep-14 6:47 
SuggestionGood but I think there's an error... Pin
Matt T Heffron17-Oct-12 10:43
professionalMatt T Heffron17-Oct-12 10:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.