Valid or Invalid Phone Number in VB.NET

Hi! This is my another tutorial in VB.NET entitled "Validating a Phone Number". We will just validate a phone number by using the System.Text.RegularExpressions namespace. So, now let's start this tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. 2. Next, add one TextBox named TextBox1 which will be used to input the phone number. Add also one Button named Button1 to know if the phone number inputted is valid or not. Design your layout like this one below: design 3. Import this namespace and put this at the top most part of your code tab:
  1. Imports System.Text.RegularExpressions<vbnet>
  2.  
  3. The namespace above provides regular expression functionality that may be used from any platform or language that runs within the Microsoft .NET Framework and it determines whether a particular string conforms to a regular expression pattern.
  4.  
  5. 4. Put this code for the Button1_Click. This will trigger to decide whether the inputted phone number is valid or not.
  6. <vbnet>
  7. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  8. Dim phonenumber As New Regex("\d{4}-\d{3}-\d{4}")
  9. If phonenumber.IsMatch(TextBox1.Text) Then
  10. MsgBox("Valid phone number!")
  11. Else
  12. MsgBox("Not Valid phone number!")
  13. End If
  14. End Sub

Explanation

We initialized phonenumber to hold the values of our regular expression ("\d{4}-\d{3}-\d{4}") which means it has a digit of 4 - digit of 3 - and digit of 4. The "d" there indicates the number of digits inside the expression. Next, we use the IsMatch function to compare the value inputted in our textbox to the regular expression of the phonenumber variable. It has to match with the expression of 0000-000-0000 format. If match, it will display "Valid phone number!" and otherwise "Not Valid phone number!".

Output:

Valid Phone Number output

Output:

Invalid Phone Number output Download the source code below and try it! :) For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards,

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer
Mobile: 09079373999
Telephone: 826-9296
E-mail:[email protected]

Visit and like my page on Facebook at: Bermz ISware Solutions

Subscribe at my YouTube Channel at: SerBermz

Add new comment