We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

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

Suppai
Suppai asked
on
Medium Priority
208 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.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
As i said before - you'd be better using a regex ;-)

Author

Commented:
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?
CERTIFIED EXPERT
Top Expert 2016

Commented:
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

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
just test the URL entered and if invalid add the http:// to the front if not there.
thats all the browsers do :)

Author

Commented:
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?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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.

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
browsers certainly don't use regexp, its not a job for a regexp

CERTIFIED EXPERT
Top Expert 2016

Commented:
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

Author

Commented:
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.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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.

Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
prepending a few prefixes is actually very simple, and you can use the URL/URI class to check it.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Top Expert 2016
Commented:
>>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

Author

Commented:
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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.