Phone Number Validate in Visual Basic .NET

October 15, 2020

You can use System.Text.RegularExpressions.Regex class to validate phone number entered in a TextBox control in VB.NET

This is the code to validate phone number in 999-999-9999 format

Imports System.Text.RegularExpressions
Public Class Form1

    Private Sub ButtonValidate_Click(sender As Object, e As EventArgs) Handles ButtonValidate.Click
        Dim phone As New Regex("\d{3}-\d{3}-\d{4}")
        If (phone.IsMatch(TextBox1.Text)) Then
            LabelMessage.Text = "Phone Number is Valid"
        Else
            LabelMessage.Text = "Phone Number is Invalid"
        End If


    End Sub
End Class

Output : Valid Number

Output : Invalid Number

You can use the RegularExpressions for other types of validation in Visual Basic.net