VB.Net Regular Expressions

Regular expressions can be very useful tools they allow you to compare any input to a set layout, and content criteria, in this example an email address is used. This is a very simple example as I found that when I first looked at regular expressions people had a tendency to overcomplicate them. on a button click Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click construct youre regular expression Dim emailregex As Regex = New Regex("[a-zA-Z0-9]+\@[a-zA-Z0-9]+\.") check if the input matches youre required criteria Dim ismatch As Boolean = emailregex.IsMatch(TextBox1.Text) display the result message If ismatch Then lblValid.Text = "Validated" Else lblValid.Text = "Not Validated" End If End Sub The regular expression is formed by putting together different criteria, this one breaks down as a-zA-Z-9 Which is any string in upper or lower case. +\@ there must be an @ symbol at this point in the string a-zA-Z-9 Again any string in upper or lower case. +\. There must be at least one . at this point Obviously this is not the strictest validation possible but it does offer a quick and fairly accurate validation that is easily customizable.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish