Solved

ASP Function to validate a URL

Posted on 2006-06-25
6
1,175 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

729 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