AndersE
AndersE

Reputation: 332

Validating e-mail with regular expression VB.Net

I'm working on a small project in VB.Net where I get a input from a textbox, and need to verify that this is an e-email address.

I found this expression "^[_a-z0-9-]+(.[_a-z0-9-]+)@[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,4})$", but i cant find any way to test if it passes.

I want some code like:

if not txtEmail.text = regexString then
    something happens..
else
    something else happens..
end if

Upvotes: 12

Views: 57717

Answers (6)

Joel Coehoorn
Joel Coehoorn

Reputation: 415921

Use the System.Text.RegularExpressions.Regex class:

Function IsEmail(Byval email as string) as boolean
    Static emailExpression As New Regex("^[_a-z0-9-]+(.[a-z0-9-]+)@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$")

    return emailExpression.IsMatch(email)
End Function

The most important thing to understand about this answer is that I didn't write the regular expression myself. There are just so many wrong ways that seem to be right, and there are several levels of detail that you could take this to. For example, do you want to restrict this to valid top level domains, and if so, how are you accounting for the fact that they are now occasionally adding new TLDs? If the regular expression the most appropriate place for that test, or should have separate code for that check? Even the expression in this answer is now very stale since it was originally authored.

I recommend finding an outside resource for the expression you know will be maintained over time.

Upvotes: 25

Rick
Rick

Reputation: 1913

Possibly off-topic since it's not a regex solution, but you could just use some of the built in features of .NET 2.0:

try
{
   MailAddress email = new MailAddress(txtEmail.Text);
}
catch(FormatException fe)
{
   // output error
}

Upvotes: 5

Jan Goyvaerts
Jan Goyvaerts

Reputation: 21999

Pick your favorite regex from my article on matching email addresses with a regex, and plug it into this Visual Basic code:

If Regex.IsMatch(SubjectString, "regex") Then
    Error = False
Else
    Error = True
End If

The best regex to match an email address is a controversial topic that I don't want to get into here. My article discusses the issues that you should be aware of when picking a regex. The regex in Joel Coehoorn's answer is definitely not a good one.

Upvotes: 6

Mick
Mick

Reputation: 13475

Email address: RFC 2822 (simplified) Matches a normal email address. Does not check the top-level domain. Requires the "case insensitive" option to be ON.

Dim FoundMatch As Boolean
Try
    FoundMatch = Regex.IsMatch(txtEmail.text, "\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase)
Catch ex As ArgumentException
    'Syntax error in the regular expression
End Try

If Not FoundMatch Then
   Error = True
Else
   Error = False
End If

Upvotes: -2

chills42
chills42

Reputation: 14513

That regex isn't really complete... in fact... most aren't (check out this article, or this one).

Unless you really enjoy pain, regex isn't the right way to validate an email address.

Upvotes: 2

Anderson Imes
Anderson Imes

Reputation: 25650

There is a great website for this kind of thing, http://regexlib.com/. Not only does it have a tester application where you can paste in a regular expression and test it, but there is also a library of regular expressions you can use with community feedback on their validity, etc. I'm not a regex guru, so I go here when I need a quick regular expression.

Also, if you are thinking of developing regular expressions yourself, there is an excellent tool called Regex Buddy that will allow you to create and test your regular expressions on the fly using an easy to understand English interpretation of your regex.

Upvotes: 5

Related Questions