Solved

ASP Function to validate a URL

Posted on 2006-06-25
6
1,167 Views
Last Modified: 2012-06-27
Hi,
I need an ASP funnction that can validate a URL from a form.
Could you pls direct me to one.
Thanx
0
Comment
Question by:dpinto04
  • 2
6 Comments
 
LVL 21

Accepted Solution

by:
ap_sajith earned 50 total points
ID: 16978488
What do you mean validate?.. If you want to check the existance of a URL, then use the function below.

<%
Response.write CheckFileExists("http://www.mysite.com/index.asp")

'==============================================================
' Function to check if a specified URL exists specified URL
' Usage : Response.write CheckFileExists("http://www.mysite.com/index.asp")
'==============================================================
Function CheckFileExists(strURL)
      Dim oXMLHTTP ' As Object
      Dim strResult ' As String
      ' Create XMLHTTP Object
      '----------------------------
      Set oXMLHTTP = CreateObject("Microsoft.XMLHTTP")    
      ' Open connection using "POST" method, asynchronously
      '-----------------------------------------------------------------
      oXMLHTTP.Open "GET", strURL, False
      ' Send HTTP request to the server
      '--------------------------------------
      oXMLHTTP.Send
      ' Error Check
      '---------------
      If Err.Number = 0 Then
            ' Wait to receive response from server
            '---------------------------------------------
            strResult = oXMLHTTP.responseText
      End If
      ' Cleanup
      '----------
      Set oXMLHTTP = nothing
      ' Return the response to the caller
      '--------------------------------------
      If strResult<>"" Then
            CheckFileExists = True
      Else
            CheckFileExists = False
      End If
End Function
%>

Hope this helps...
Cheers!
0
 

Author Comment

by:dpinto04
ID: 16978514
By validate I mean check if its a URL that exists and does not some random stuff typed into the form

The above script gives me:
msxml3.dll (0x800C0005)
The system cannot locate the resource specified.
0
 
LVL 9

Expert Comment

by:david_levine
ID: 16978810
Here's an example - http://levinecentral.com/test/url_exists.asp

Here's the source:
<html>
<head><title>Check if URL exists</title></head>
<body>
<form method="POST" action="url_exists.asp">
      <p><input type="text" name="formURL" size="40" value="<%=request.form("formURL")%>"</p>
      <p><input type="submit" value="Check" name="B1"></p>
      <%
      if request.form("formURL") > "" then
            response.write("<hr><br>")
            Dim objXMLHTTP, strReturnStatus
            Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
            objXMLHTTP.Open "GET", request.form("formURL"), False
            objXMLHTTP.Send
            strReturnStatus = objXMLHTTP.Status
            Set objXMLHTTP = Nothing
            
            if strReturnStatus = "200" then
                  response.write("URL " & request.form("formURL") & " was found.")
            else
                  response.write("URL " & request.form("formURL") & " was NOT found.")
            end if
      end if
      %>
</form>
</body>
</html>
0
 
LVL 9

Expert Comment

by:david_levine
ID: 16978839
Here's the same code with an "on error" to handle missing domains..

<html>
<head><title>Check if URL exists</title></head>
<body>
<form method="POST" action="url_exists.asp">
      <p><input type="text" name="formURL" size="40" value="<%=request.form("formURL")%>"</p>
      <p><input type="submit" value="Check" name="B1"></p>
      <%
      On Error Resume Next
      if request.form("formURL") > "" then
            response.write("<hr><br>")
            Dim objXMLHTTP, strReturnStatus
            Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
            objXMLHTTP.Open "GET", request.form("formURL"), False
            objXMLHTTP.Send
            strReturnStatus = objXMLHTTP.Status
            Set objXMLHTTP = Nothing
            
            if strReturnStatus = "200" then
                  response.write("URL " & request.form("formURL") & " was found.")
            else
                  response.write("URL " & request.form("formURL") & " was NOT found.")
            end if
      end if
      %>
</form>
</body>
</html>
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now