Validate URL programmatically using asp.net C#

Posted by Gopal_nivas under ASP.NET on 10/31/2011 | Points: 10 | Views : 6602 | Status : [Member] | Replies : 5
hi..

i need to validate URL using c#.

the url must contain extension of a specific types like pdf,doc,docx,jpg etc.

so i need to validate the URL as well as it must contain extension.

how to do that..
need ur suggetsions with examples
regards
gopal.s




Responses

Posted by: Mdjack on: 10/31/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Gopal,

try the following example

string url = "www.google.com"; //here u can assign ur URL
if(UrlIsValid(url)) {
Response.Write("The URL '" + url + "' is valid.");
} else {
Response.Write("The URL '" + url + "' is NOT valid.");
}

public static bool UrlIsValid(string HostName)
{
bool br = false;
try {
IPHostEntry ipHost = Dns.Resolve(HostName);
br = true;
}
catch (SocketException se) {
br = false;
}
return br;
}



N. MOHAMED ZACKKARIAH

Gopal_nivas, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Blessyjees on: 10/31/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,

could you specify some sample urls what you want to validate.

Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Gopal_nivas, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gopal_nivas on: 10/31/2011 [Member] Starter | Points: 25

Up
0
Down
hi blessyjees,
thanks for ur reply..
sample urls are below.
http://www.xm.ie/aboutxml/wordsample2.doc
http://www.aeab.org/sample_refrences.pdf

need ur suggestions with examples
regards
gopal.s



Gopal_nivas, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 11/1/2011 [Member] Starter | Points: 25

Up
0
Down
Try to use a regular expression validator something like
Regex.IsMatch("sitename", @"^[\w]+(.doc|.docx|.pdf)$")


Gopal_nivas, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mittaikariss on: 11/2/2011 [Member] Bronze | Points: 25

Up
0
Down
Regex re = new Regex(@"^www.[a-zA-Z0-9]{1,15}.(com|org|ie|doc|docx|pdf)$");


if (!re.IsMatch(txturl.Text))

{

MessageBox.Show("..........................................");

}


^www.[a-zA-Z0-9]{1,15}.(com|org|ie|doc|docx|pdf)$


http://www.invomtech.com/

....sundarsowmia....

Gopal_nivas, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response