Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,392 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,241 people online right now. Registration is fast and FREE... Join Now!




validateing a telephone number in vb.net

 
Reply to this topicStart new topic

validateing a telephone number in vb.net, all i have is a few bits a code yet it can't seem to find out why

zeronekox
6 Feb, 2008 - 07:19 AM
Post #1

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 8


My Contributions
CODE

Dim AreaCode, Exchange, Number As Int16

        If TextBox1.Text.Length >= 13 Then
            MsgBox("Phone numbers must be 12 digits")
            TextBox1.Text = ""
            TextBox1.Focus()
            Exit Sub
        End If

        If TextBox1.Text.Substring(3, 1) <> "-" Then
            MsgBox("Phone Numbers must be in a ###-###-#### format")
            TextBox1.Text = ""
            TextBox1.Focus()
            Exit Sub

        End If

        If TextBox1.Text.Substring(7, 1) <> "_" Then
            MsgBox("Phone Numbers must be in a ###-###-#### format")
            TextBox1.Text = ""
            TextBox1.Focus()
            Exit Sub

        End If

        AreaCode = TextBox1.Text.Substring(0, 3)
        Exchange = TextBox1.Text.Substring(4, 3)
        Number = TextBox1.Text.Substring(8, 4)



        MsgBox("The number is a phone number. " & AreaCode & " " & Exchange & " " & Number)
    End Sub


EDIT: Please use code tags when posting your code, it makes the code much easier to read smile.gif Code tags are used like => code.gif

This post has been edited by PsychoCoder: 6 Feb, 2008 - 09:05 AM
User is offlineProfile CardPM
+Quote Post

sharpy
RE: Validateing A Telephone Number In Vb.net
6 Feb, 2008 - 08:02 AM
Post #2

D.I.C Head
Group Icon

Joined: 2 Jun, 2007
Posts: 204



Thanked: 2 times
Dream Kudos: 275
My Contributions
Not sure what you are doing. Place a button on your form then add the code into the button sub then run the application.

Your code ran for me


CODE

Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim AreaCode, Exchange, Number As Int16

        If TextBox1.Text.Length >= 13 Then
            MsgBox("Phone numbers must be 12 digits")
            TextBox1.Text = ""
            TextBox1.Focus()
            Exit Sub
        End If

        If TextBox1.Text.Substring(3, 1) <> "-" Then
            MsgBox("Phone Numbers must be in a ###-###-#### format")
            TextBox1.Text = ""
            TextBox1.Focus()
            Exit Sub

        End If

        If TextBox1.Text.Substring(7, 1) <> "_" Then
            MsgBox("Phone Numbers must be in a ###-###-#### format")
            TextBox1.Text = ""
            TextBox1.Focus()
            Exit Sub

        End If

        AreaCode = TextBox1.Text.Substring(0, 3)
        Exchange = TextBox1.Text.Substring(4, 3)
        Number = TextBox1.Text.Substring(8, 4)



        MsgBox("The number is a phone number. " & AreaCode & " " & Exchange & " " & Number)
    End Sub
End Class


Hope this helps

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Validateing A Telephone Number In Vb.net
6 Feb, 2008 - 09:19 AM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,476



Thanked: 160 times
Dream Kudos: 9025
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
The easiest way, and most efficient way to do this would be to use a regular expression. First, add this to the top of your file:


CODE

Imports System.Text.RegularExpressions



Then use a regular expression to make sure the phone number is in the correct format, like so:


CODE

Public Function ValidatePhone(ByVal num As String) As Boolean
        'create our regular expression pattern
        Dim pattern As String = "^[1-9]\d{2}-[1-9]\d{2}-\d{4}$"
        'create our regular expression object
        Dim check As New Regex(pattern)
        Dim valid As Boolean = False
        'Make sure a phone number was provided
        If Not String.IsNullOrEmpty(num) Then
            valid = check.IsMatch(num)
        Else
            valid = False
        End If
        Return valid
End Function



Then to use the Function, do that like so:


CODE

If Not ValidatePhone(TextBox1.Text) Then
     MsgBox("Phone Numbers must be in a ###-###-#### format")
      TextBox1.Text = ""
      TextBox1.Focus()
Else
      'Do somthing else
End If



Hope this helps, and it's a lot less code smile.gif

Happy Coding!
User is online!Profile CardPM
+Quote Post

zeronekox
RE: Validateing A Telephone Number In Vb.net
13 Feb, 2008 - 07:02 AM
Post #4

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 8


My Contributions
well it all helped out a worked thank you both very much and more to you phyco
User is offlineProfile CardPM
+Quote Post

sharpy
RE: Validateing A Telephone Number In Vb.net
13 Feb, 2008 - 11:05 AM
Post #5

D.I.C Head
Group Icon

Joined: 2 Jun, 2007
Posts: 204



Thanked: 2 times
Dream Kudos: 275
My Contributions
No Problem

Glad to help however Phycocoder normally will show us all up with his Knowledge and has also helped me loads biggrin.gif
User is offlineProfile CardPM
+Quote Post

imms
RE: Validateing A Telephone Number In Vb.net
24 Aug, 2008 - 04:30 AM
Post #6

New D.I.C Head
*

Joined: 24 Aug, 2008
Posts: 1

hi Dear ,
Can anybody tell me how can i format Phone No string while some time have have variable lenth of digits Like

+966-*-********
or
+92-42-7496226
So please let me know what to do for validation and how can i use regular expression

Regards,

Bhatti
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:07AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month