Normalizing Email Addresses
I was going to write a post here describing this function, why I wrote it, and what it does. But then I re-read the comments that I'd added to the function and the examples I'd included in the code and said "Hey, this is perfectly documented and doesn't need any further explanation."
/// <summary>
/// Takes an email address and converts it to a normalized base form.
/// This is needed for some email addresses such as gmail where you can
/// add dots anywhere in the name as well as a + sign and tag anything
/// extra on to the email address.
/// Example email addresses to normalize:
/// is.this.an.email@gmail.com
/// is.this.an.email+extrainfo@gmail.com
/// isthisanemail+extrainfo@gmail.com
/// These should all normalize to:
/// isthisanemail@gmail.com
/// </summary>
/// <param name="emailAddress">Any email address</param>
/// <returns>A modified/normalized email address or the original string</returns>
static public string NormalizeEmailAddress(string emailAddress)
{
// So far we only know about gmail.com that does this
if(emailAddress.Contains("@gmail.com"))
{
string[] parts = emailAddress.Split('@');
// If there are more than 2 @'s in the address then this is an
// invalid email address so don't try and do anything to it.
if (parts.Count() == 2)
{
string[] beforeAtParts = parts[0].Split('+');
return beforeAtParts[0].Replace(".","") + "@" + parts[1];
}
}
return emailAddress;
}
Ironically I love this gmail feature and I use it all the time and I think that most people use it for the good. Unfortunately there's a small element of the population who will use this for multiple registrations on sites so that they can troll and spam so I am doing this conversion as a preventitive measure. Note that on the sites that I've implemented this on I've still allowed the users to register with any address that they want. However, when comparing a registration attempt against a site that does not allow registrations with the same email address then this will prevent multiple registrations by the same person.