View All Blog Posts

Validate Domain Names

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;    
        }
            
    }
}
| Tuesday, January 4, 2011 |