Tag Archives: REGEX

How to Validate an Email Address in VB.NET using REGEX

If you have a need to validate a user input or string to ensure that only a valid email is entered , then you can use Regular Expressions or REGEX  to handle that easily.

REGEX expressions appear to be quite complicated but once you have a valid syntax then it is simply a matter of using the proper syntax of REGEX in the following function and you should also be able to use the same function block to handle other types of inputs as well. eg. Numeric

 

The following function will validate your input against a valid email address.

Step 1

At the top of your module or form add the following line of code

Imports System.Text.RegularExpressions

Step 2
Add the following function in your form

Function Validate_Email(ByVal value As String) As Boolean
Dim bok As Boolean = False
Dim s As String
s = LCase(value)

Dim myMatch As Match = System.Text.RegularExpressions.Regex.Match(s, “^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$”)

If myMatch.Success Then
bok = True

Return bok
Else
bok = False
Return bok
End If
End Function

Step 3

How to use the function

Dim bok as boolean

bok = Validate_Email( YourInputTextHere)

If the user input is a valid email then bok will be true else false.