I was looking at the Util class that we use in our website and decided to put the code here on the blog. I'll create a post for each method, but be aware, the code isn't always the most optimized and some of it was written over 6 years ago.
Let's start with something simple
public static bool LegalEmail(string email) {
if (email == null) return false;
if (email.Length < 6) return false;
if (email.IndexOf("@") == -1) return false;
return (Regex.Match(email, @"^[a-z0-9\._-]+@[a-z0-9\._-]+\.+[a-z0-9]{2,4}$", RegexOptions.IgnoreCase).Length != 0);
}
This method simply checks if the string is a legal email address. Now that .Net support extension in v3.5 I guess it's time to change this into an extension, so instead of writing
string email = "example@example.com";
if (Util.LegalEmail(email)) {
//do stuff
}
you can write
string email = "example@example.com";
if (email.LegalEmail()) {
//do stuff
}