[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1178
  • Last Modified:

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
0
dpinto04
Asked:
dpinto04
  • 2
1 Solution
 
ap_sajithCommented:
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
 
dpinto04Author Commented:
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
 
david_levineCommented:
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
 
david_levineCommented:
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!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now