Validate Phone Number- VB.NET Regex

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

In this tutorial, I use regex to determine if a phone number entered by a user is entered in the correct format. Validate phone number in this format: 999-999-9999
validate phone number in visual basic .net
validate phone number vb.net

Imports System.Text.RegularExpressions
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles Button1.Click
        Dim phoneNumber As New Regex("\d{3}-\d{3}-\d{4}")
        If phoneNumber.IsMatch(TextBox1.Text) Then
            TextBox2.Text = "Valid phone number"
        Else
            TextBox2.Text = "Not Valid phone number"
        End If
    End Sub
End Class

Validate phone number in this format (999)999-9999
Private Sub Button1_Click_1(sender As System.Object, _
 e As System.EventArgs) Handles Button1.Click
        Dim phoneNumber As New Regex("\(\d{3}\)\d{3}-\d{4}")
        If phoneNumber.IsMatch(TextBox1.Text) Then
            TextBox2.Text = "Valid phone number"
        Else
            TextBox2.Text = "Not Valid phone number"
        End If
End Sub

vb.net validate phone number


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.