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!

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.

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.