Validating a Phone Number
Simple example of validating a phone number:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Invalid phone number
'Dim strPhone As String = "123456789"
' valid phone number
Dim strPhone As String = "(123) 456-7890"
Dim regexp As New Regex(("^\(?\d{3}\)?\s|-\d{3}-\d{4}$"))
If regexp.IsMatch(strPhone) Then
MsgBox("Valid Phone Number")
Else
MsgBox("Invalid Phone Number")
End If
End Sub
Comments
- Anonymous
August 23, 2004
The comment has been removed - Anonymous
September 12, 2004
Ummm...I think your regex will match if the area code has only one parenthesis - is this what you want?
Also, I think your test for whitespace of a dash should be (s|-).
And finally, I think it fails to match if the dash between the exchange and number is missing - is this what you want?
You might want to take advantage of some of the regex testing programs that are available on the net. I use the Regex Coach - very cool!
Good luck... - Anonymous
September 12, 2004
Yes - in this instance i was looking for this exact format. Any other instances or permutations I expected it to fail. Of course this can be expanded to include a variety of other formats.
Thanks