12

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
AndersE
  • 332
  • 1
  • 4
  • 9
  • 3
    It's a nice question to have on record at StackOverflow - I see nothing wrong with asking this here. – Jeffrey Dec 15 '08 at 20:59

6 Answers6

25

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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 3
    `If … Then Return True Else Return False` is surely in the all-time top ten of anti-patterns. :-/ – Konrad Rudolph Dec 15 '08 at 20:13
  • Yeah: it's a bad habit I acquired at my last gig, where policy prevented returning the results of boolean expressions directly. Fix the sample. – Joel Coehoorn Dec 15 '08 at 20:16
  • I found that it needs to be "As New System.Text.RegularExpressions.Regex" where it says "As New Regex" Thx for the help :) – AndersE Dec 15 '08 at 20:22
  • 1
    Expression is broken. See http://stackoverflow.com/questions/369543/validating-e-mail-with-regular-expression-vbnet#369614 – bzlm Dec 15 '08 at 20:41
  • 3
    Regex to match e-mail addresses is inherently broken. You can generate the *feeling* that you pretty much cover all your bases, but in the end there will nearly always be a valid pattern that you refuse, or an invalid one you let though. Yours does not cover apostrophes as valid, but they are. – Tomalak Dec 15 '08 at 20:45
  • The dots in that regex definitely need to be escaped. But the reges is inadequate for other reasons too. – Jan Goyvaerts Dec 17 '08 at 14:59
  • Should be: "^[_a-z0-9-]+(.[a-z0-9-]*)@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$" –  Aug 15 '14 at 14:59
  • 3
    If … Then Return True Else Return False is 10 times more readable than Return Matchobject. which is what it effectively is what you are doing above.. you have no idea what the return type is just by looking at it, you could be 400 lines of code away from the as boolean. Readability trumps your "perfect pattern" of minimalistic code wastage.. any day. better readability reduces bugs. Most programmers who are arrogant about their perfect patterns tend to have the most bugs, based on my 25yrs of experience. – hamish Sep 26 '14 at 23:21
  • The regex in the op above failed on a First.Name@Company.com email so I used this: "^((([!#$%&'+\-/=?^_{|}~\w])|([!#$%&'*+\-/=?^_{|}~\w][!#$%&'‌​*+\-/=?^_{|}~\.\w]{0‌​,}[!#$%&'*+\-/=?^_{|‌​}~\w]))[@]\w+([-.]\w‌​+)*\.\w+([-.]\w+))$" Source regex and test page here: regexlib.com/RETester.aspx?regexp_id=2558 – Jeff Mergler Aug 22 '17 at 17:05
6

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.

Jan Goyvaerts
  • 21,379
  • 7
  • 60
  • 72
5

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.

Anderson Imes
  • 25,500
  • 4
  • 67
  • 82
  • I had a case where an email was failing the simpler regex above "^[_a-z0-9-]+(.[a-z0-9-]+)@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$". Returning back here I used regexlib that Anderson was kind enough to share and searched for the highest rated "email" regexs. There were 4 hits and this one was the top of the list and worked best for me: "^((([!#$%&'*+\-/=?^_`{|}~\w])|([!#$%&'*+\-/=?^_`{|}~\w][!#$%&'*+\-/=?^_`{|}~\.\w]{0,}[!#$%&'*+\-/=?^_`{|}~\w]))[@]\w+([-.]\w+)*\.\w+([-.]\w+)*)$" source and test page here: http://regexlib.com/RETester.aspx?regexp_id=2558 – Jeff Mergler Aug 22 '17 at 16:19
5

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
}
Rick
  • 1,863
  • 2
  • 19
  • 46
  • 1
    This is everywhere in the project I'm working on at the moment and I'm ripping it out. I don't see how throwing an exception is good logic or validation technique, apart from anything it's an expensive task. Feel free to correct me though – matt_lethargic May 01 '14 at 14:00
  • I had a hard time getting this to break, I passed it all sorts of garbage emails and it gobbled them up without exception and I removed this from my code. – Jeff Mergler Aug 22 '17 at 16:27
  • If you do this with an email address of, for example, `john. doe@anywhere.com`, with a space after `john.`, the MailAddress object will accept the text and `MailAddress.Address` will have a value of `doe@anywhere.com`, without throwing an exception. – HardCode Oct 08 '21 at 12:56
2

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.

chills42
  • 14,201
  • 3
  • 42
  • 77
-2

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
Mick
  • 13,248
  • 9
  • 69
  • 119
  • Expression is broken (for example, doesn't allow tld-only-addresses). See http://stackoverflow.com/questions/369543/validating-e-mail-with-regular-expression-vbnet#369614 – bzlm Dec 15 '08 at 20:43
  • Example is not broken. It does not check top level domains. As I mentioned in my comment. See my other comment for one that does. – Mick Dec 15 '08 at 20:57
  • 3
    I will never understand why anybody would write an IF statement like your the last one in this snippet. That's just insane. It should be: Error = Not FoundMatch – Josh Stodola Jun 03 '09 at 17:22
  • Yes that would be briefer. I was going for general clarity. So now you know why anybody would write an IF statement like the last one in this snippet :-) – Mick Jun 03 '09 at 21:04