hyperlink validation for linklabel

obscuregirl

Member
Joined
Sep 13, 2006
Messages
15
Location
UK
Programming Experience
Beginner
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:

VB.NET:
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!
 
It seems to be tough finding a definitive list of the protocols. What you have may be the best way to handle the situation. I know of at least one other protocol that can make up a URL: MMS. It would be interesting to find a comprehensive list, but a quick search didn't turn up much. As for the "www", you do not always need it. The link to my website doesn't have one, and using it in my case will not get you to my website. Hope this helps.
 
Back
Top