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