Skip to content

Instantly share code, notes, and snippets.

@janierdavila
Last active December 13, 2015 20:48
Show Gist options
  • Select an option

  • Save janierdavila/4972622 to your computer and use it in GitHub Desktop.

Select an option

Save janierdavila/4972622 to your computer and use it in GitHub Desktop.
Email validation. Found this on MSDN somewhere and wanted to keep it handy
using System;
using System.Globalization;
using System.Text.RegularExpressions;
namespace Utility
{
public class RegexUtilities
{
static bool _invalid;
public static bool IsValidEmail(string strIn)
{
_invalid = false;
if (String.IsNullOrEmpty(strIn))
return false;
// Use IdnMapping class to convert Unicode domain names.
try
{
strIn = Regex.Replace(strIn, @"(@)(.+)$", DomainMapper,
RegexOptions.None, TimeSpan.FromMilliseconds(200));
}
catch (RegexMatchTimeoutException)
{
return false;
}
if (_invalid)
return false;
// Return true if strIn is in valid e-mail format.
try
{
return Regex.IsMatch(strIn,
@"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$",
RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
}
catch (RegexMatchTimeoutException)
{
return false;
}
}
private static string DomainMapper(Match match)
{
// IdnMapping class with default property values.
IdnMapping idn = new IdnMapping();
string domainName = match.Groups[2].Value;
try
{
domainName = idn.GetAscii(domainName);
}
catch (ArgumentException)
{
_invalid = true;
}
return match.Groups[1].Value + domainName;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment