![]() |
| ||||||||
|
|||||||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 |
|
Active Warrior
Join Date: Jan 2009
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
|
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 |
|
|
|
|
|
#2 |
|
Active Warrior
Join Date: Nov 2009
Location: UK
Posts: 40
Thanks: 0
Thanked 5 Times in 5 Posts
|
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
|
|
|
|
|
|
#3 |
|
Active Warrior
Join Date: Jan 2009
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
|
i am working in vb.net writing an exe program. I am using a webbrowser control, text box and a button.
|
|
|
|
|
|
#4 |
|
Warrior Member
Join Date: Oct 2009
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
|
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. |
|
|
|
|
|
#5 |
|
Warrior Member
Join Date: Nov 2009
Posts: 20
Thanks: 0
Thanked 3 Times in 3 Posts
|
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. |
|
|
|
|
|
#6 |
|
Advanced Warrior
War Room Member
Join Date: Dec 2008
Posts: 702
Thanks: 214
Thanked 56 Times in 49 Posts
|
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.
|
|
|
|
|
|
#7 |
|
Active Warrior
Join Date: Jan 2009
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
|
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 |
|
|
|
|
|
#8 | |
|
Advanced Warrior
War Room Member
Join Date: Sep 2008
Location: Honolulu, Hawaii, USA (Currently in Montreal Canada)
Posts: 813
Blog Entries: 1
Thanks: 138
Thanked 217 Times in 146 Posts
|
Quote:
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 | |
|
|
|
|
|
#9 |
|
Active Warrior
Join Date: Jan 2009
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
|
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.
|
|
|
|
|
|
#10 |
|
www.linkabyss.com
War Room Member
Join Date: Oct 2007
Location: , , United Kingdom.
Posts: 833
Thanks: 18
Thanked 71 Times in 60 Posts
|
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? |
|
NEW Online Article Creator! Now includes brand NEW content research feature!
FREE Article Rewriting Tool - Rewrite Existing Articles With Ease! Brand NEW Tool Just Launched! Brand NEW SEO Software Launching Soon! Link ABYSS One Way Link Building System - Offering Full Control And Flexibility Of Where You Obtain Your Links |
|
|
|
|
|
|
#11 |
|
Active Warrior
War Room Member
Join Date: Jan 2009
Posts: 74
Thanks: 7
Thanked 4 Times in 4 Posts
|
CURL:
PHP Code:
|
|
Warrior Forum Special Offer:
Viral-CPA - Unleash A Traffic Storm On Your Website Today - Over $1000 In 24 hours!!! |
|
|
|
|
|
|
#12 |
|
Malik
War Room Member
Join Date: Oct 2009
Posts: 222
Thanks: 7
Thanked 25 Times in 24 Posts
|
You can try this regex.
CODE-one linear ($urlregex): PHP Code:
it will validates all this type of url ($url) PHP Code:
PHP Code:
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]
-Malik PS. please tell me if it is wrong to paste the code here. and wait for some feedbacks |
|
Last edited by sanjid112; 11-07-2009 at 03:47 AM. Reason: adding PS |
|
|
|
|
|
|
#13 |
|
Warrior Member
Join Date: Nov 2009
Posts: 20
Thanks: 0
Thanked 3 Times in 3 Posts
|
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? |
|
|
|
![]() |
|
| Tags |
| url, valadating |
| Thread Tools | |
|
|
![]() |