Link to home
Start Free TrialLog in
Avatar of dpinto04
dpinto04

asked on

ASP Function to validate a URL

Hi,
I need an ASP funnction that can validate a URL from a form.
Could you pls direct me to one.
Thanx
ASKER CERTIFIED SOLUTION
Avatar of ap_sajith
ap_sajith

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dpinto04
dpinto04

ASKER

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.
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>
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>