Skip to content

Instantly share code, notes, and snippets.

@jarrettmeyer
Created May 16, 2012 12:05
Show Gist options
  • Save jarrettmeyer/2709855 to your computer and use it in GitHub Desktop.
Save jarrettmeyer/2709855 to your computer and use it in GitHub Desktop.
Email validation - the point is to show that the extension method doesn't actually do anything. The real logic is in the validator class.
public class EmailValidator
{
private const string PATTERN = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$";
public static bool IsValid(string s)
{
if (string.IsNullOrEmpty(s))
return false;
// Regex from http://www.regular-expressions.info/email.html
bool isMatch = Regex.IsMatch(s, PATTERN, RegexOptions.IgnoreCase);
return isMatch;
}
}
public static class StringExtensions
{
public static bool IsEmail(this string str)
{
return EmailValidator.IsValid(str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment