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

x
?
Solved

How do you make an URL input accept non-strict urls like e.g. "www.google.coM"

Posted on 2009-02-12
13
Medium Priority
?
195 Views
Last Modified: 2013-11-23
My application gives the user the opportunity to input a list of urls. I want the application to check whether a url is valid when the user adds it. I tried using the URL object however it only accepts it when the http:// prefix is included. Is there a way around that so that the user can input the urls without that like e.g. in most common internet browsers? What i specifically want is when the user has entered the url in simple form then the url is converted to full form with the http prefix and added to the lsit. The toExternalForm method of the URL object seems to do the trick, but it doesnt help much if the URL object can't be instantiated at all without the http:// prefix.
0
Comment
Question by:Suppai
  • 5
  • 4
  • 4
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 23625402
As i said before - you'd be better using a regex ;-)
0
 

Author Comment

by:Suppai
ID: 23625939
Ahh I missed that. However you have to elaborate on how I can use that in Java? You mean using the classes found in java.util.regex? There must be a method for what I want to do out there somewhere, I mean isnt it a rather often used operation that I want to perform?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23626030
e.g.

http://snipplr.com/view/2371/regex-regular-expression-to-match-a-url/

Generally you will have to double backslashes for Java


final String RE = <the regex>
 
boolean valid = url.matches(RE);

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 92

Expert Comment

by:objects
ID: 23626624
just test the URL entered and if invalid add the http:// to the front if not there.
thats all the browsers do :)
0
 

Author Comment

by:Suppai
ID: 23627526
Isnt it a bit more sophisticated? I mean if I do that and the user inputs e.g. "google.com" then it would become "http://google.com" which I suspect isnt valid as well?
0
 
LVL 92

Expert Comment

by:objects
ID: 23627558
a little bit, but not much
Basically it just has a list of prefeix to try adding. So in the above case it just adds http://www.

0
 
LVL 92

Expert Comment

by:objects
ID: 23627589
browsers certainly don't use regexp, its not a job for a regexp

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23627641
Browsers don't validate urls, so they're not good reference points for what you want to do. All they'll do is apply some crude heuristics to save the user some typing.

Validation should be done:

a (if you have the time) by trying to connect to it. If you can't, it's probably pretty useless anyway
b. using a regex
0
 

Author Comment

by:Suppai
ID: 23627715
Yeah I guess in the end it is the users responsibility to type in the url correctly. But I get the point: use the quite thorough regex method that is maybe a little bit out of the scope of the school project I'm doing in which it is not a crucial part, OR provide browser like adding of prefixes such as http:// if not accepted by the URL object or "http://www" if "www" is furthermore njot found in the potential url String. I think I might stick with error handling at the point when the app is going to connect I think, it will be easier. But thanks for the vbaluable input, have to go, will provide credits tmw.
0
 
LVL 92

Expert Comment

by:objects
ID: 23627784
regex is out of the scope and doesn't help you. Its not going going to help anymore then paring with URL/URI class. In fact it could make matters worse.

0
 
LVL 92

Accepted Solution

by:
objects earned 750 total points
ID: 23627829
prepending a few prefixes is actually very simple, and you can use the URL/URI class to check it.

0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 750 total points
ID: 23627880
>>might stick with error handling at the point when the app is going to connect

Much better idea - and what i originally suggested. 'validation' actually could amount to a waste of time anyway
0
 

Author Closing Comment

by:Suppai
ID: 31546243
Using the the Internet Explorer system for adding a home url, I found that yes the system is very simple, "http://" is merely added if not detected in the string, so if a big browser like IE can live with that I suppose I have a good argument for sticking with that as well if my lecturer should ask:-) As for the actual url checking I'll stick with error handling when trying to connect. In the end its the users responsibility to key in the right urls.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month19 days, 21 hours left to enroll

872 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