[2005] hyperlink validation for linklabel
Hi
I'm not sure sure if this is the most appropriate forum for this question so if I'm in the wrong place, my apologies!
I have a text field in my application where a user can enter a URL. Once this URL has been validated, a linklabel is shown next to the textbox which has a hyperlink to the URL in the textbox.
My validation code looks like this at the moment:
Code:
Dim str_mess As String
str_mess = ""
If Not theURL.StartsWith("http://") And Not theURL.StartsWith("https://") And Not theURL.StartsWith("ftp://") Then
theURL = "http://" & theURL
End If
Try
Dim myReq As HttpWebRequest = WebRequest.Create(theURL)
Dim myHttpWebResponse As HttpWebResponse = CType(myReq.GetResponse(), HttpWebResponse)
myHttpWebResponse.Close()
Catch e As WebException
str_mess = e.Message
MessageBox.Show("You have not entered a valid URL", "URL Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Finally
If str_mess IsNot "" Then
validateURL = False
Else
validateURL = True
End If
End Try
This works ok as far as it goes, but if I put in a URL without the www. prefix, it validates ok but won't navigate to the page when I click on the link.
My knowledge of internet protocols is sketchy to say the least and the stuff I've read so far has just confused me more! Some people seem to insist that any site will work without the www. whilst others are adamant that it's needed. I also know that there are other domains out there aside from http, https and ftp, but wouldn't know where to start with finding out what they are.
Does anyone know any way of getting some kind of definitive list so that I can validate all possibilities when it comes to URLs? Either that, or do you know a more elegant way of handling this validation? Thanks!
Re: [2005] hyperlink validation for linklabel
http, https and ftp are protocols. The domain is just the "name.com" bit and the owner of that domain can prefix anything they like to it. It's convention to prefix "www" for a World Wide Web page but there's no specific requirement for it.
Re: [2005] hyperlink validation for linklabel
Quote:
Originally Posted by jmcilhinney
http, https and ftp are protocols. The domain is just the "name.com" bit and the owner of that domain can prefix anything they like to it. It's convention to prefix "www" for a World Wide Web page but there's no specific requirement for it.
oops! showed my ignorance a bit more than I meant to there! thanks jmcilhinney, that's really useful info.
I'm a bit confused by the www not being specifically required. If I type in the URL of www.bbc.co.uk, clicking on my linklabel takes me straight to that site but if I try the same thing for bbc.co.uk, it brings up an error. Maybe I'm using the wrong thing to click on the link? the code I'm using is:
Code:
System.Diagnostics.Process.Start(txtJournURL.Text)
Do you know of a better way to handle this? thanks.
Re: [2005] hyperlink validation for linklabel
If the domain owner has chosen to prefix their domain with "www" then you have to use "www" to access it. What I meant was that there is no requirement of the domain owner that they use "www". They could just use "mydomain.com" or "bigfathairypig.mydomain.com" if they wanted.
Re: [2005] hyperlink validation for linklabel
Quote:
Originally Posted by jmcilhinney
If the domain owner has chosen to prefix their domain with "www" then you have to use "www" to access it. What I meant was that there is no requirement of the domain owner that they use "www". They could just use "mydomain.com" or "bigfathairypig.mydomain.com" if they wanted.
I see..... so there's not really any totally foolproof way of checking that a URL is correct..... I think I'll just display a warning message to say that not including 'www' in the URL may mean the link doesn't work. Then it's up to the users! Thanks for your help!