Saturday, January 24, 2004

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


11:03:03 PM    

I was recently putting together some proof of concept for a customer and realized that I hadn't posted one of the common Regular Expressions that I had been using.

Validating a Zip Code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       
' Invalid Zip code
        
'Dim strZip As String = "AB123"

       
' valid Zip Code
      
Dim strZip As String = "03110"

     
Dim regexp As New Regex(("^\d{5}$"))
    
     
If regexp.IsMatch(strZip) Then
                  
MsgBox("Zip Code")
    
Else
                 
MsgBox("No zip match")
    
End If

End Sub


10:53:15 PM    

Found an excellent cheat sheet for Regular Expressions. Well worth checking out.

http://www.regexlib.com/CheatSheet.htm


10:26:02 PM