-1

I have been given a task to detect whether the written string is a valid URL or not. And if it is a valid URL, make it an active link.

E.g.: if for example I write hello www.djfh.com then it should detect that this is a valid URL and linkify it and make it an active URL.

If I write wwwwww.jvhdfj.fd then it should not linkify it as it is not a valid URL.

How do I make a link depicted as an active URL in java using jquery or JS or any other logic.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Kanika
  • 27
  • 2
  • 1
    jQuery validate has a url validator. Once it's validated you just need to append an `a` element to the DOM with the `href` set to the validated value. For more specific help, please post the code you have tried. – Rory McCrossan Sep 26 '13 at 10:33
  • Google for 'validate URL in [language]' – SubjectCurio Sep 26 '13 at 10:35
  • In what context do you need to validate the url? You are tagging the question with both `java` and `javascript`, are you planning to do this clientside or serverside? Are you familiar with regular expressions? Please clarify these questions so people can post valuable answers. – Justus Romijn Sep 26 '13 at 10:43
  • yes i am familiar with reg expressions..i want to know that once we have validated with regex that the written string a valid url or not how do i linkify that specific url..how to depict to the user that it is an active link – Kanika Sep 26 '13 at 10:59
  • @ justus i want it to be client side..as the user writes on the wall any string the validator should detect whether it is a valid url or not..and if it is then it should make it an active link so that the user does not have to copy paste the link in the browser to open it,rather the user should be able to click on that valid link and shou;d be able to open the link from there itself. – Kanika Sep 26 '13 at 11:45

2 Answers2

0

To detect if the URL valid you could use Regular Expressions

The Pattern would be Something like this : "www/..+./com" (can be different in other langsames, and it's a verry simple Pattern

Teifun2
  • 338
  • 2
  • 15
  • can you please be a little elaborative in this code snippet..if for example i have my portal wall and my friend writes on my wall page – Kanika 3 mins ago edit contd: writes on my wall page www.abc.com then it should be detected that it is a valid url or not and once detected valid it should be shown as a link..and if not valid then it should be shown as a normal string. – Kanika Sep 26 '13 at 10:51
  • can you please help as in how to linkfy the active urls.. – Kanika Sep 26 '13 at 10:57
0

you can use

$url = "http://www.example.com";

if(!filter_var($url, FILTER_VALIDATE_URL))
{
echo "URL is not valid";
}
else
{
echo "URL is valid";
}
Sunil Verma
  • 2,490
  • 1
  • 14
  • 15
  • can you please be a little elaborative in this code snippet..if for example i have my portal wall and my friend writes on my wall page – Kanika Sep 26 '13 at 10:47
  • contd: writes on my wall page www.abc.com then it should be detected that it is a valid url or not and once detected valid it should be shown as a link..and if not valid then it should be shown as a normal string.. – Kanika Sep 26 '13 at 10:48
  • Please help in this..if on using a regex expression once validated how to linkify that link – Kanika Sep 26 '13 at 10:56
  • if you want to first get urls from a string or say what anybody have written on your wall you can use preg_match as follows: $mpmatch = "#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#is"; $str = "this is my friend's website http://example.com I think it is coll"; preg_match_all($mpmatch, $str, $result); foreach ($result as $val) { echo "
    ";
      print_r($val);
     } then you can again check these extracted urls by the function above to be 100% sure. hope this helps
    – Sunil Verma Sep 26 '13 at 10:56
  • you can also use callback on preg_match_all as follows: http://php.net/manual/en/function.preg-replace-callback.php – Sunil Verma Sep 26 '13 at 10:58