Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 5th Nov 2009, 12:12 PM   #1
HyperActive Warrior
War Room Member
 
NicheExposed's Avatar
 
Join Date: 2009
Posts: 221
Thanks: 1
Thanked 54 Times in 16 Posts
Default
valadating a url
Share on: 
fb share twitter share gplus share more share

Thanks for everyone who helped me with the first problem but I have one more. I need to validate that a url is typed into the text box before a user clicks on the button to send the url to the webbrowser control.

basically I need it to do this....

When a user types in the complete url http://www.yoursite.com and then hits the go button it will take them to the url. But if they type in anything but a valid url I want it to display a message box and preventing them from visiting a page. If anyone types in anything but a valid url the program will crash. How do I validate that it is a valid url and not garbage.

Thanks
NicheExposed is offline  
Unread 5th Nov 2009, 02:01 PM   #2
Active Warrior
War Room Member
 
jwlnewsome's Avatar
 
Join Date: 2009
Location: UK
Posts: 51
Thanks: 0
Thanked 7 Times in 7 Posts
Default
Re: valadating a url
Share on: 
fb share twitter share gplus share more share

Hi, try pinging the url before you preform the action. There are a few php scripts out there that can ping addresses to see if there is a response. I would personally cURL the url to see if i get anything back other than a 404 error
jwlnewsome is offline  
Unread 5th Nov 2009, 02:17 PM   #3
HyperActive Warrior
War Room Member
 
NicheExposed's Avatar
 
Join Date: 2009
Posts: 221
Thanks: 1
Thanked 54 Times in 16 Posts
Default
Re: valadating a url
Share on: 
fb share twitter share gplus share more share

i am working in vb.net writing an exe program. I am using a webbrowser control, text box and a button.
NicheExposed is offline  
Unread 5th Nov 2009, 05:28 PM   #4
Warrior Member
 
Join Date: 2009
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default
Re: valadating a url
Share on: 
fb share twitter share gplus share more share

You can do something like this:

If Texbox1.Text.Substring(0,7) = "hxxp://" Or Textbox1.Text.Substring(0, 4) = "wwvv." Then
MessageBox.Show("This is a URL")
End If

The code checks the first letters of your Textbox1, if it begins with hxxp:// or wwvv. then it is deemed as a valid URL.

If you need more help just let me know, I can help when I have free time.

EDIT:
Change the xx to tt and vv to w, the forum wouldn't let me post a link due to my post count.
warmlikecoffee is offline  
Unread 5th Nov 2009, 06:02 PM   #5
Active Warrior
War Room Member
 
Mike P Smith's Avatar
 
Join Date: 2009
Location: Charlotte, NC, USA
Posts: 30
Thanks: 2
Thanked 4 Times in 3 Posts
Default
Re: valadating a url
Share on: 
fb share twitter share gplus share more share

If you really want a challenge (and earn your geek stripes) try using "Regular Expressions" - I think there are libraries available for VB.NET if that is your platform.

That can validate a URL rather well with just one statement.

--
Mike Smith
No sig, no shirt, no sale.
Mike P Smith is offline  
Unread 5th Nov 2009, 08:48 PM   #6
Advanced Warrior
War Room Member
 
Join Date: 2008
Posts: 811
Thanks: 247
Thanked 69 Times in 59 Posts
Default
Re: valadating a url
Share on: 
fb share twitter share gplus share more share

Although I`m not too familiar with vb.net, I`m assuming its similar to VB6. Simply navigate to the URL, and if nothing is there, then you know its not valid. I.e., webbrowser1.navigate \"whatever.com\" then check the \'location\' attribute, etc.
Johnathan is offline  
Unread 6th Nov 2009, 04:04 PM   #7
HyperActive Warrior
War Room Member
 
NicheExposed's Avatar
 
Join Date: 2009
Posts: 221
Thanks: 1
Thanked 54 Times in 16 Posts
Default
Re: valadating a url
Share on: 
fb share twitter share gplus share more share

I ended up calling a programming friend of mine who gave me the code. I had to create a function. here is the code if anyone else needs it.


PrivateFunction UrlIsValid(ByVal url AsString) AsBoolean
Dim is_valid AsBoolean = False
If url.ToLower().StartsWith("www.") Then url = _
"http://" & url
Dim web_response As HttpWebResponse = Nothing
Try
Dim web_request As HttpWebRequest = _
HttpWebRequest.Create(url)
web_response = _
DirectCast(web_request.GetResponse(), _
HttpWebResponse)
ReturnTrue
Catch ex As Exception
ReturnFalse
Finally
IfNot (web_response IsNothing) Then _
web_response.Close()
EndTry
EndFunction
NicheExposed is offline  
Unread 6th Nov 2009, 04:43 PM   #8
VIP Warrior
War Room Member
 
ereadergold's Avatar
 
Join Date: 2008
Location: Montreal Canada
Posts: 2,390
Thanks: 1,017
Thanked 836 Times in 572 Posts
Blog Entries: 1
Default
Re: valadating a url
Share on: 
fb share twitter share gplus share more share

Originally Posted by NicheExposed View Post

If anyone types in anything but a valid url the program will crash.
Just a thought, you might consider writing an Error Handler as well - having the program crash if it receives a bad URL is not particularly user-friendly!

Keep in mind that the user may enter a valid URL by clicking on an invalid hyperlink within a web page. That bad URL will probably make it past your URL format check.

Beat of luck with your project...

Bill
ereadergold is offline  
Unread 6th Nov 2009, 05:39 PM   #9
HyperActive Warrior
War Room Member
 
NicheExposed's Avatar
 
Join Date: 2009
Posts: 221
Thanks: 1
Thanked 54 Times in 16 Posts
Default
Re: valadating a url
Share on: 
fb share twitter share gplus share more share

when someone clicks on a link in a web page that is bad will just return a page can not be displated error. That has nothing to do with the coding problem within the software.
NicheExposed is offline  
Unread 6th Nov 2009, 05:50 PM   #10
andyblackseo.com
War Room Member
 
AndyBlackSEO's Avatar
 
Join Date: 2007
Location: , , United Kingdom.
Posts: 1,623
Thanks: 62
Thanked 199 Times in 146 Posts
Default
Re: valadating a url
Share on: 
fb share twitter share gplus share more share

For php you would use regex (regular expression) to determine whether a url is in a valid format. You could then go one further by using cURL to grab the contents of the page and verify that a valid live webpage exists at the end of it. I'm creating this functionality for my latest product and it works perfectly.

What platform are you wanting to code this in? web app or a windows application?

[FREE SEO TOOL] Build 29 Effective, High Authority Backlinks that Will Increase Your Google Rankings in 2020... CLICK HERE ...
... Instant backlinks that can get you results within 24-72hrs.
AndyBlackSEO is offline  
Unread 6th Nov 2009, 09:01 PM   #11
Active Warrior
War Room Member
 
Join Date: 2009
Posts: 77
Thanks: 13
Thanked 7 Times in 6 Posts
Default
Re: valadating a url
Share on: 
fb share twitter share gplus share more share

CURL:

PHP Code:
function url_exists()
    {
    
VAR1   = curl_init();
    if (
false === VAR1)
        {
        return 
false;
        }
    
curl_setopt(VAR1, CURLOPT_HEADER, true);
    
curl_setopt(VAR1, CURLOPT_FAILONERROR, true);
    
curl_setopt(VAR1, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") );
    
curl_setopt(VAR1, CURLOPT_NOBODY, true);
    
curl_setopt(VAR1, CURLOPT_RETURNTRANSFER, true);
    
VAR2  = curl_exec(VAR1);
    
curl_close(VAR1);  
    if (
preg_match('/200 OK/i',substr_replace(VAR2,'',30))) {
        return 
true;
    }else{
        return 
false;
        }
    } 
Replace VAR1 with $ something and VAR2 with $ something else (WF wont let me post up proper variables)
Gimme4Free is offline  
Unread 7th Nov 2009, 03:46 AM   #12
HyperActive Warrior
War Room Member
 
sanjid112's Avatar
 
Join Date: 2009
Posts: 176
Thanks: 3
Thanked 28 Times in 18 Posts
Default
Re: valadating a url
Share on: 
fb share twitter share gplus share more share

You can try this regex.

CODE-one linear ($urlregex):
PHP Code:
   = "^(https?|ftp)://([a-z0-9+!*(),;?&=.-]+(:[a-z0-9+!*(),;?&=.-]+)?@)?[a-z0-9+-]+(.[a-z0-9+-]+)*(:[0-9]{2,5})?(/([a-z0-9+-].?)+)*/?(?[a-z+&.-][a-z0-9;:@/&%=+.-]*)?(#[a-z_.-][a-z0-9+.-]*)?$"; 
if (
eregi(, )) {echo "good";} else {echo "bad";} 
(OPTIONAL: Read more for some explanations

it will validates all this type of url ($url)
PHP Code:
// valid urls 
 
= "https://user:pass@www.somewhere.com:8080/login.php?do=login&style=%23#pagetop"; 
 = 
"http://user@www.somewhere.com/#pagetop"; 
 = 
"https://somewhere.com/index.html"; 
 = 
"ftp://user:****@somewhere.com:21/"; 
 = 
"http://somewhere.com/index.html/";  //this is valid!! 
The CODE-broken into section for easy editing and understanding ($urlregex):
PHP Code:
// SCHEME 
 
= "^(https?|ftp)://"; 

// USER AND PASS (optional) 
 
.= "([a-z0-9+!*(),;?&=.-]+(:[a-z0-9+!*(),;?&=.-]+)?@)?"; 

// HOSTNAME OR IP 
 
.= "[a-z0-9+-]+(.[a-z0-9+-]+)*";  // http://x = allowed (ex. http://localhost, http://routerlogin) 
// .= "[a-z0-9+-]+(.[a-z0-9+-]+)+";  // http://x.x = minimum 
// .= "([a-z0-9+-]+.)*[a-z0-9+-]{2,3}";  // http://x.xx(x) = minimum 
//use only one of the above 

// PORT (optional) 
 
.= "(:[0-9]{2,5})?"; 
// PATH  (optional) 
 
.= "(/([a-z0-9+-].?)+)*/?"; 
// GET Query (optional) 
 
.= "(?[a-z+&.-][a-z0-9;:@/&%=+.-]*)?"; 
// ANCHOR (optional) 
 
.= "(#[a-z_.-][a-z0-9+.-]*)?$"; 

// check 
if (eregi(, )) {echo "good";} else {echo "bad";} 
All the lines can be safely removed (except for the hostname) if you do not want allowing some URL segment (if you don't want getqueries in your urls, just comment the respective $urlregex .= ....) - but do not reorder them.
the "(optional)" states that the part MAY exist, but url will be valid even if it doesn't contain the part (see the valid urls above).

syntax:
Code:
<http[s]|ftp> :// [user[:pass]@] hostname [port] [/path] [?getquery] [anchor]
  • take into account allowed safe characters
  • assumption .. (double dot) is never allowed in hostname or path.

-Malik

PS. please tell me if it is wrong to paste the code here. and wait for some feedbacks

Last edited on 7th Nov 2009 at 03:47 AM. Reason: adding PS
sanjid112 is offline  
Unread 7th Nov 2009, 07:26 PM   #13
Active Warrior
War Room Member
 
Mike P Smith's Avatar
 
Join Date: 2009
Location: Charlotte, NC, USA
Posts: 30
Thanks: 2
Thanked 4 Times in 3 Posts
Default
Re: valadating a url
Share on: 
fb share twitter share gplus share more share

Nice post, sanjid112.

For VB.NET, using sanjid112's regex, this is what you need:

If Not Regex.IsMatch(myURL, "^(https?|ftp)://([a-z0-9+!*(),;?&=.-]+(:[a-z0-9+!*(),;?&=.-]+)?@)?[a-z0-9+-]+(.[a-z0-9+-]+)*(:[0-9]{2,5})?(/([a-z0-9+-].?)+)*/?(?[a-z+&.-][a-z0-9;:@/&%=+.-]*)?(#[a-z_.-][a-z0-9+.-]*)?$")Then
' write your error code
' here.
Else
' write your success code here
End If

--
Mike Smith
No advertising in my Sig. What am I thinking?
Mike P Smith is offline  
Closed Thread


Bookmarks

Tags
url, valadating


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 10:30 AM.