Check URL syntax and write HREF tag

Benny Sutton

Check URL syntax and write HREF tag

Skip Navigation LinksHome :: Classic ASP :: Check URL/write href tag

Here's a simple Vb function you can use on active server pages to ensure a string is a fully formed URL and automatically write it into a HREF tag. Great for user input stored in a database. People always forget to add the "http://". It needs improving I know!

Call like this: GetWebsiteFromURL("www.UnderwaterPhotography.com")
Returns this: www.UnderwaterPhotography.com

<%
Function GetWebsiteFromURL(strURL)
    If Left(strURL, 11) = "http://www." Or Left(strURL, 7) = "http://" Then 'assume OK
        GetWebsiteFromURL = "<a href=""" & strURL & """ target=_blank>" & strURL & "</a>"
    ElseIf Left(strURL, 4) = "www." Then
        GetWebsiteFromURL = "<a href=""" & "http://" & strURL & """ target=_blank>" & strURL & "</a>"
    ElseIf strURL = "" Then
    End If
End Function
%>

cut and paste the Visual Basic code below onto an Active Server Page
Next >> Join Request.Form to Querystring