70 lines
1.6 KiB
C#
70 lines
1.6 KiB
C#
using System.Collections.Generic;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace BitStrap
|
|
{
|
|
public static class EmailHelper
|
|
{
|
|
private const string emailPattern = "^([0-9a-zA-Z]([\\+\\-_\\.][0-9a-zA-Z]+)*)+@(([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]*\\.)+[a-zA-Z0-9]{2,17})$";
|
|
|
|
private static ICollection<string> emailProviders;
|
|
|
|
static EmailHelper()
|
|
{
|
|
emailProviders = new HashSet<string>();
|
|
emailProviders.Add("gmail.com");
|
|
emailProviders.Add("hotmail.com");
|
|
emailProviders.Add("yahoo.com");
|
|
emailProviders.Add("live.com");
|
|
emailProviders.Add("icloud.com");
|
|
emailProviders.Add("me.com");
|
|
emailProviders.Add("outlook.com");
|
|
}
|
|
|
|
public static bool IsEmail(string text)
|
|
{
|
|
return Regex.IsMatch(text, "^([0-9a-zA-Z]([\\+\\-_\\.][0-9a-zA-Z]+)*)+@(([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]*\\.)+[a-zA-Z0-9]{2,17})$");
|
|
}
|
|
|
|
public static bool IsMistyped(string email, out string correctEmail)
|
|
{
|
|
if (!IsEmail(email))
|
|
{
|
|
correctEmail = null;
|
|
return true;
|
|
}
|
|
correctEmail = email;
|
|
int num = email.IndexOf('@') + 1;
|
|
int num2 = email.IndexOf('.', num);
|
|
int num3 = email.IndexOf('.', num2 + 1);
|
|
if (num3 < 0)
|
|
{
|
|
num3 = email.Length;
|
|
}
|
|
num2 = num3 - num;
|
|
string source = email.Substring(num, num2);
|
|
string value = null;
|
|
int num4 = 6;
|
|
foreach (string emailProvider in emailProviders)
|
|
{
|
|
int num5 = source.Distance(emailProvider);
|
|
if (num5 == 0)
|
|
{
|
|
return false;
|
|
}
|
|
if (num5 < num4)
|
|
{
|
|
num4 = num5;
|
|
value = emailProvider;
|
|
}
|
|
}
|
|
if (!string.IsNullOrEmpty(value))
|
|
{
|
|
correctEmail = email.Remove(num, num2).Insert(num, value);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|