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!
Related Article: How to validate string format vb.net
is a solved VB.NET discussion thread by jbutardo that has 10 replies, was last updated 1 year ago and has been tagged with the keywords: format, string, validation, vb.net.
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