Validate Domain Names

4 January 2011

Going one step further we can validate the domain names by testing each row to see if there is a web site on the end with this simple Dynamic Column in Data Sync 3.0.

using System.Net;
class DataSourceRowOverride : DataSourceRowInternal
{    
    public string EmailDomain { get { return GetDomainName(Email); } }
    public bool IsValidDomainName { get { return IsValidDomain(EmailDomain); } }
    public string GetDomainName(string value) 
    {
        var expression = new Regex(@"[A-Z0-9._%-]+@([A-Z0-9.-]+\.[A-Z]{2,4})", RegexOptions.IgnoreCase);
        
        var match = expression.Match(value);
        if ( match.Success )
            return match.Result("$1");
            
        return value;
    }
    public bool IsValidDomain(string domainName) 
    {
        try 
        {
            var request = HttpWebRequest.Create("http://www." + domainName);
            request.Timeout = 1000;
            using(var response = request.GetResponse()) 
            {
                
            }    
            return true;
        } 
        catch(WebException)
        {
            return false;    
        }
            
    }
}

| |