Regular Expression Validation for Email Address

In almost all cases, it is a good practice to validate the users input because of many reasons (i.e. security, reliability, as proof of the given template, etc.).

There is a quick and simple way to check if a text box entry contains a valid email address. A valid email address being something in the form of user@domain.com
There are a number of characters that are not permitted in the email address, and a requirement of a period ‘ . ‘  to occur atleast once after the @ symbol.

Below is the Regular Expression (RegEx) string that can be applied to a .NET RegularExpressionValidator object or a customised function

Dim MatchEmailPattern as String = "^(([w-]+.)+[w-]+|([a-zA-Z]{1}|[w-]{2,}))@" _
     & "((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9]).([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])." _
     & "([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9]).([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|" _
     & "([a-zA-Z0-9]+[w-]+.)+[a-zA-Z]{2,4})$"

You can then apply this RegEx to a RegularExpressionValidator object

RegularExpressionValidator1.ValidationExpression=MatchEmailPattern

Or create your own customised function to verify if an email address is valid

Public Function isValidEmail(ByVal EmailAddress As String) As Boolean
        If Not String.IsNullOrEmpty(EmailAddress) Then Return Regex.IsMatch(EmailAddress, MatchEmailPattern)
        Return False
End Function