Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Create a function in a module to verify phone numbers in accordance with the North American Numbering Plan (NANP) rules. You should be able to pass in a phone number and the function will verify whether the phone number is valid. There are many ways to do this. My suggestion would be to have the function return a string value. If the string value returned is an empty string then the phone number is valid. If the phone number is invalid have the function return the string with the reason why the phone number is invalid. Below are some sample invalid phone numbers, representing each class of number:

Verify a specific phone number manually entered against NANP rules
Use a MakedTexbox for the phone number entry. Don’t forget to strip the added characters “()-

must use option strict on and Module that holds a function

there will be a box for the phone number, a button to validate, a button to close

no Hungarian notation
---I need to call the module function on my form

What I have tried:

module code:
VB
Option Strict On
Module functionmodule
    '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).
    ''' 
    ''' <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.
    ''' <returns>Returns a Boolean value indicating whether the specified number is a valid
    ''' phone number in accordance with the North American Number Plan (NANP).
    ''' 
    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 = 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 IsMatch(NPA, NRegEx) OrElse Not IsMatch(NXX, NRegEx) _
                     OrElse Not 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

    Private Function IsMatch(xXXX As String, xXXXRegEx As String) As Boolean
        Throw New NotImplementedException()
    End Function

End Module
Posted
Updated 5-Aug-17 4:01am
v2
Comments
PIEBALDconsult 5-Aug-17 9:49am    
And?

1 solution

Here you can find some Regex expressions for NANP validation: Regular Expression Library[^]
Here is a MaskedTextBox example: [dotnetperls]
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900