Email Validation Tool in Visual Basic

Introduction: This tutorial is on how to make an email addres validator tool in Visual Basic. Design: This tool will contain; a textbox, named defaultly textbox1, this is where the string to be validated will be enetered. a button, named defaulty button1, this is how the validation process will begin. Imports: To validate the string as an email, we are going to be using RegEx which requires it's own import from...
  1. Imports System.Text.RegularExpressions
RegEx String: Then on the button1 click we first create a RegEx string to match our string against...
  1. Dim emailRegex As New System.Text.RegularExpressions.Regex("^(?<user>[^@]+)@(?<host>.+)$")
This regex - emailRegex - is an official Microsoft Visual Basic regex to ensure that any strings parsed through this method are properly validated to an email. The Match: Now that we have our Regex string to match our string against, we can create a new Regex Match variable - essentially a boolean - which contains whether the parsed string is a match to the earlier created regex - emailRegex - we parse the string entered in to the first textbox - textbox1 - by using the .text property of the textbox1 textbox...
  1. Dim emailMatch As System.Text.RegularExpressions.Match = emailRegex.Match(TextBox1.Text)
Output: Finally we can output the results. We msgbox "This email is valid." if the entered string is in an email format, or "This is not a valid email" if not...
  1. If (emailMatch.Success) Then
  2. MsgBox("This email is valid.")
  3. Else : MsgBox("This is not a valid email.")
  4. End If
Full Source: Done. Here is the full source!
  1. Imports System.Text.RegularExpressions
  2.  
  3. Public Class Form1
  4. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  5. Dim emailRegex As New System.Text.RegularExpressions.Regex("^(?<user>[^@]+)@(?<host>.+)$")
  6. Dim emailMatch As System.Text.RegularExpressions.Match = emailRegex.Match(TextBox1.Text)
  7. If (emailMatch.Success) Then
  8. MsgBox("This email is valid.")
  9. Else : MsgBox("This is not a valid email.")
  10. End If
  11. End Sub
  12. End Class

Add new comment