0

I have the following code and I want it to check whether or not an email address is in an acceptable format but for some reason the real email addresses I feed it still reply that the email address is invalid. The email address is in string format so unsure as to why I am getting matches.

if (!Regex.IsMatch(u1.EmailAddress, @"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b"))
{
    MessageBox.Show("email is invalid");
}
AGB
  • 2,230
  • 1
  • 14
  • 21
Naive
  • 21
  • 1
  • to modify your regex \b[A-z0-9._%+-]+@[A-z0-9.-]+\.[A-z]{2,}\b https://regex101.com/r/gK9sS6/1 Might want to check out: http://stackoverflow.com/questions/3194407/extract-all-email-addresses-from-some-txt-documents-using-ruby – Skyler Jokiel Apr 18 '16 at 15:27
  • I would not try to do this myself. Look it up on the net, here's an example: https://msdn.microsoft.com/en-us/library/01escwtf(v=vs.110).aspx – Carra Apr 18 '16 at 15:31
  • I would take this approach, http://stackoverflow.com/questions/1365407/c-sharp-code-to-validate-email-address/16403290#16403290 – Mike Miller Apr 18 '16 at 15:33

1 Answers1

3

You regex only matches to address written in UPPERCASE letter, digits and some symbols.

Use this instead :

Regex.IsMatch(u1.EmailAddress, @"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b", RegexOptions.IgnoreCase)
Xiaoy312
  • 14,292
  • 1
  • 32
  • 44
  • From regex101.com library, "Match Email" : `/^(([a-zA-Z]|[0-9])|([-]|[_]|[.]))+[@](([a-zA-Z0-9])|([-])){2,63}[.](([a-zA-Z0-9]){2,63})+$/gi` – Apolo Apr 18 '16 at 15:41
  • The `/__PATTERN__/__MODIFIERS__` doesn't work in c#. Modifiers are replaced by `RegexOptions`. Idk, but `/[a-zA-Z]/i` and `[-]|[_]|[.]` seems redundant. – Xiaoy312 Apr 18 '16 at 15:57