Email Validation Tool in Visual Basic
Submitted by GeePee on Friday, March 27, 2015 - 23:20.
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...
RegEx String:
Then on the button1 click we first create a RegEx string to match our string against...
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...
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...
Full Source:
Done. Here is the full source!
- Imports System.Text.RegularExpressions
- Dim emailRegex As New System.Text.RegularExpressions.Regex("^(?<user>[^@]+)@(?<host>.+)$")
- Dim emailMatch As System.Text.RegularExpressions.Match = emailRegex.Match(TextBox1.Text)
- If (emailMatch.Success) Then
- MsgBox("This email is valid.")
- Else : MsgBox("This is not a valid email.")
- End If
- Imports System.Text.RegularExpressions
- Public Class Form1
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim emailRegex As New System.Text.RegularExpressions.Regex("^(?<user>[^@]+)@(?<host>.+)$")
- Dim emailMatch As System.Text.RegularExpressions.Match = emailRegex.Match(TextBox1.Text)
- If (emailMatch.Success) Then
- MsgBox("This email is valid.")
- Else : MsgBox("This is not a valid email.")
- End If
- End Sub
- End Class
Add new comment
- 79 views