Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How i can check an string to be a correct web site format?

 
raminaa niilian
Ranch Hand
Posts: 551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thank you for reading my post.
i want to check an string to see whether it is a correct web site name or not.
I know that i should use regular expression , but i do not know how i should do this , can some one help me with this ?

Thanks
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a regular expression.

However, I think it would be better to try to construct a java.net.URL object from the String. If it its a valid URL, the construction will succeed. If it is badly-formed, the constructor will throw MalformedURLException.

That will be good enough to detect many common errors in your URL. However, you may wish to go further. If the java.net.URL object is successfully constructed, you can then check whether the protocol is appropriate for a Web site. You can check this with the getProtocol() method; the protocol should be "http". You could also check that a host has been given, by testing whether the getHost() method returns a non-null, non-empty string.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic