2

I have an input form where user passes in a URL. Currently there is no validation but now need one to verify if URL is a really valid web link or not. I couldn't find any built-in URL validators in play framework. If inputted URL isn't valid then throw an error message to the user. How to go about doing this in play?

user2066049
  • 1,371
  • 1
  • 12
  • 26

1 Answers1

1

Use Java URL class and catch MalformedURLException to detect an invalid URL. Simply instantiate the class with passed string new URL(formUrl) http://docs.oracle.com/javase/6/docs/api/java/net/URL.html

Information about how to implement a custom form validation can be found here: http://www.playframework.com/documentation/2.2.x/ScalaCustomValidations

Rado Buransky
  • 3,252
  • 18
  • 25
  • Thanks. This does look like a cleaner API http://commons.apache.org/proper/commons-validator/apidocs/org/apache/commons/validator/UrlValidator.html . Any recommendation against it? – user2066049 Mar 13 '14 at 11:59
  • You probably shouldn't use `java.net.URL` use `java.net.URI` instead. See: https://stackoverflow.com/questions/9607903/get-domain-name-from-given-url/9608008#9608008 – nVitius Aug 30 '17 at 22:45