Validating domain name in ASP.NET

SheoNarayan
Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 23400 red flag

In this article, I have tried to get the solution of checking a valid domain name in ASP.NET. This might not be the 100% full proof method to validate the valid domain however based on the number of test I did, it works.


 Download source code for Validating domain name in ASP.NET

Introduction

Checking valid email address is often common using RegularExpression in ASP.NET, however in case user has entered abc@dfasdfasdfasd.com, the email validation will pass however this is not the correct email id as the domain name itself is not valid.

In this article, I have tried to create a function that will check for a domain and if it exists it writes success message otherwise failure. It can be tweaked to return true and false too!

Creating ValidateDomain function

Picture 1 - Validation for domain name passed.

To use the function, I am going to write, you need to use following namespaces.

using System.Net;

using System.IO;

My sample UI looks like above picture. In order to check for a valid domain, I have written below code that fires on the click event of the button.

Get solutions of your .NET problems with video explanations and source code in .NET How to's.

protected void CheckForValidDomain(object sender, EventArgs e)

{

string url = "http://www." + txtDomain.Text.Trim();

WebResponse response = null;

string data = string.Empty;

try

{

WebRequest request = WebRequest.Create(url);

response = request.GetResponse();

using (StreamReader reader = new StreamReader(response.GetResponseStream()))

{

data = reader.ReadToEnd();

}

// domain exists, this is valid domain

lblText.Text = "<font color=\"green\"><b>Valid domain name !</b></font>";

}

catch (WebException ee)

{

// return false, most likely this domain doesn't exists

lblText.Text = "<br /><b>Most probably this domain name doesn't exists.</b> <br />" + ee.ToString();

}

catch (Exception ee)

{

// Some error occured, the domain might exists

lblText.Text = "Error occured. " + ee.ToString();

}

finally

{

if (response != null) response.Close();

}

txtBox1.Text = data;

}

In this function, I have stored the user entered domain name in the url variable prefixing with http://www and made a request using WebRequest in-built class, stored the response into WebResponse object. After getting the response, I read it using StreamReader and stored the output into a string variable. After that in the last line of this function, I have set the multiline textbox value to the response I have received.

So when the valid domain name exists, things will work fine without any error and will write "Valid domain name"! message and the source code of the page will be written into the multiline textbox. But if the domain name doesn't exists then the request.GetResponse() line will throw error of type WebException that I have caught into the first catch block where I am assuming that this domain name is not valid/doesn't exists.

But if the error will be of some different type not related with the domain name or resolving the server then it will fall into the 2nd catch block where I have shown simple generic error message.

Caution: 

Note that in this methodology, even if there will be a network issue from your server you will get the WebException and this function will return false or Invalid domain. So you will have to use some different methodology to check if network connection exists and there is a WebException then that domain is invalid .




In order to run my demo page, you need to keep following config into your web.config file (only if you are using ASP.NET 4.0).

<httpRuntime requestValidationMode="2.0"/>

Source Code

The source code of this example can be downloaded from the download link. Feel free to manipulate it according to your requirement and please respond to this article with your updated code if that one is better than mine so that everybody gets benefited.

Thanks for reading and keep reading and sharing your knowledge on DotNetFunda.Com !

Page copy protected against web site content infringement by Copyscape

About the Author

SheoNarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com
Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)