?
Solved

ASP Function to validate a URL

Posted on 2006-06-25
6
Medium Priority
?
1,176 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 200 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
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 brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

771 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