We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,277 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Validating Phone Number Format in TextBox

I'm trying to validate the correct format for a phone number in a text box: (000) 000-0000

For a test, i made it so that a messagebox shows up saying "correct" if the format is correct. This test works well WHEN the format is correct. BUT when the format IS NOT correct, I keep getting an error saying that the index was out of the array.

How can I fix it? Here's what I have...

ElseIf phonenumbertxtbox.Text.Length <> 0 Then
            If phonenumbertxtbox.Text.Chars(0) <> "(" And phonenumbertxtbox.Text.Chars(4) <> ")" Then
                MessageBox.Show("incorrect")
            Else
                MessageBox.Show("correct")
            End If

I only tested the (000) because I wanted to see if i could get it to work before completing the code.

Please help! Thank you!

4
Contributors
3
Replies
1 Year
Discussion Span
1 Year Ago
Last Updated
6
Views
Question
Answered
yongj
Junior Poster in Training
83 posts since May 2010
Reputation Points: 11
Solved Threads: 1
Skill Endorsements: 0

You Might look into using regular expressions or the Like operator:

If phonenumbertxtbox.Text Like "$$$-$$$-$$$$" Then Msgbox ("Correct")

Regular expressions are more advanced, but you can do more checking with them.

Xcelled194
Junior Poster in Training
90 posts since Oct 2010
Reputation Points: 41
Solved Threads: 15
Skill Endorsements: 0

See if this helps.

'//------- Pre-requisites: 1 Button, 1 TextBox named "phonenumbertxtbox". -------\\
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        With phonenumbertxtbox
            If Not .Text = Nothing Then '// check if not empty.
                If .Text.Length > 0 AndAlso .Text.Substring(0, 1) = "(" Then '// check if length is greater than 0 and if first char. is "(".
                    If .Text.Length > 4 AndAlso .Text.Substring(4, 1) = ")" Then '// check if length is greater than 4 and if 5th char. is ")".
                        If .Text.Length > 8 AndAlso .Text.Substring(8, 1) = "-" Then '// check if length is greater than 8 and if 9th char. is "-".
                            MsgBox("Correct")
                        Else : MsgBox("Incorrect")
                        End If
                    Else : MsgBox("Incorrect")
                    End If
                Else : MsgBox("Incorrect")
                End If
            End If
        End With
    End Sub


    Private Sub phonenumbertxtbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles phonenumbertxtbox.KeyPress
        Select Case e.KeyChar
            Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "(", ")", "-", vbBack '// your pre-selected Characters and Backspace.
                e.Handled = False '// allow.
            Case Else
                e.Handled = True '// block.
        End Select
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        phonenumbertxtbox.MaxLength = 13 '// set max. allowed characters.
    End Sub
End Class
codeorder
Postaholic
2,124 posts since Aug 2010
Reputation Points: 256
Solved Threads: 387
Skill Endorsements: 8
Question Answered as of 2 Years Ago by codeorder and Xcelled194

validating textbox in vb.net email_id,mobile num,name,date,address plz give me the code

nitin3
Newbie Poster
1 post since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0644 seconds using 2.69MB