Valid or Invalid Phone Number in VB.NET
Submitted by donbermoy on Friday, February 28, 2014 - 17:13.
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:
3. Import this namespace and put this at the top most part of your code tab:
- Imports System.Text.RegularExpressions<vbnet>
- 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. Put this code for the Button1_Click. This will trigger to decide whether the inputted phone number is valid or not.
- <vbnet>
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim phonenumber As New Regex("\d{4}-\d{3}-\d{4}")
- If phonenumber.IsMatch(TextBox1.Text) Then
- Else
- End If
- 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 NumberOutput:
Invalid Phone Number 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
- 3761 views