• We just launched and are currently in beta. Join us as we build and grow the community.

Email Validation Tool in Visual Basic

saif881999

Metaverse Content Creator
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
158
Likes
37
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
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. Public

    Class Form1
  3. Private

    Sub

    Button1_Click(sender As

    Object

    , e As

    EventArgs) Handles Button1.Click
  4. Dim

    emailRegex As

    New

    System.Text.RegularExpressions.Regex("^(?<user>[^@]+)@(?<host>.+)$"

    )
  5. Dim

    emailMatch As

    System.Text.RegularExpressions.Match = emailRegex.Match(TextBox1.Text)
  6. If

    (emailMatch.Success) Then
  7. MsgBox("This email is valid."

    )
  8. Else

    : MsgBox("This is not a valid email."

    )
  9. End

    If
  10. End

    Sub
  11. End

    Class

 

452,496

332,845

332,853

Top