VB.NET || How To Validate A Phone Number Using VB.NET
The following is a module with functions which demonstrates how to validate a phone number using VB.NET.
Not only does this function check formatting, but follows NANP numbering rules by ensuring that the first digit in the area code and the first digit in the second section are 2-9.
1. Validate Phone Number
The example below demonstrates the use of ‘Utils.IsValidPhoneNumber‘ to determine if a phone number is valid.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
' Validate Phone Number Dim numbers = New List(Of String) From { "(123) 456-7890", "(123)456-7890", "123-456-7890", "123.456.7890", "1234567890", "+31636363634", "075-63546725", "(657) 278-2011", "(657)278-2011", "(657)2782011", "6572782011", "+6572782011", "657-278-2011", "657 278 2011", "657.278.2011", "1657.278.2011", "+6572782011", "16572782011", "657-2782011" } For Each number In numbers Debug.Print($"Number: {number}, Is Valid: {Utils.IsValidPhoneNumber(number)}") Next ' expected output: ' Number: (123) 456-7890, Is Valid: False ' Number: (123)456-7890, Is Valid: False ' Number: 123-456-7890, Is Valid: False ' Number: 123.456.7890, Is Valid: False ' Number: 1234567890, Is Valid: False ' Number: +31636363634, Is Valid: False ' Number: 075-63546725, Is Valid: False ' Number: (657) 278-2011, Is Valid: True ' Number: (657)278-2011, Is Valid: True ' Number: (657)2782011, Is Valid: True ' Number: 6572782011, Is Valid: True ' Number: +6572782011, Is Valid: True ' Number: 657-278-2011, Is Valid: True ' Number: 657 278 2011, Is Valid: True ' Number: 657.278.2011, Is Valid: True ' Number: 1657.278.2011, Is Valid: True ' Number: +6572782011, Is Valid: True ' Number: 16572782011, Is Valid: True ' Number: 657-2782011, Is Valid: True |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 3, 2020 ' Taken From: http://programmingnotes.org/ ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Public Module modUtils ''' <summary> ''' Not only checks formatting, but follows NANP numbering rules ''' by ensuring that the first digit in the area code and the first ''' digit in the second section are 2-9. ''' </summary> ''' <param name="phoneNumber">The phone number to verify</param> ''' <returns>True if the phone number is valid, false otherwise</returns> Public Function IsValidPhoneNumber(phoneNumber As String) As Boolean If String.IsNullOrWhiteSpace(phoneNumber) Then Return False End If Dim pattern = "^[\+]?[{1}]?[(]?[2-9]\d{2}[)]?[-\s\.]?[2-9]\d{2}[-\s\.]?[0-9]{4}$" Static regex As New Text.RegularExpressions.Regex(pattern, Text.RegularExpressions.RegexOptions.Compiled Or Text.RegularExpressions.RegexOptions.IgnoreCase) Return regex.IsMatch(phoneNumber) End Function End Module End Namespace ' http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 3, 2020 ' Taken From: http://programmingnotes.org/ ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace ' ============================================================================ Option Strict On Option Explicit On Imports System Module Program Sub Main(args As String()) Try Dim numbers = New List(Of String) From { "(123) 456-7890", "(123)456-7890", "123-456-7890", "123.456.7890", "1234567890", "+31636363634", "075-63546725", "(657) 278-2011", "(657)278-2011", "(657)2782011", "6572782011", "+6572782011", "657-278-2011", "657 278 2011", "657.278.2011", "1657.278.2011", "+6572782011", "16572782011", "657-2782011" } For Each number In numbers Display($"Number: {number}, Is Valid: {Utils.IsValidPhoneNumber(number)}") Next Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply