jnaqsh / nerdnews

A free and open source social news website focusing on computer science and FOSS news for Persian community
http://nerdnews.ir/
43 stars 13 forks source link

[insisting] on URL validation check #38

Open bijanebrahimi opened 11 years ago

bijanebrahimi commented 11 years ago

as previously mentioned here http://bit.ly/Y3hJD8, it is necessary to have URL existence validation. if you think it will cause some sort of annoying delay, you can simply put it in view (and run it on client side) using javascript.

IMPORTANT: when the news will be briefly available by the supplied referenced link, it is vital to check if it's a valid one before sending stories.

a snippet-code/guide for developers:

$('story[source]').on('blur', function({
   news_resource_url = $(this).attr('value');
   //~ you can first check for the URL validation Regex Here
   if(url_validation_check(news_resource_url)){
      //~ and when if it has a Valid URL schema, check for url HTTP response
      $.ajax({ type: 'head', url: news_resource_url,
         success: function() {
            // URL exists
            $('story[source]').parent().parent().removeClass('error');
         }, error: function() {
           // page does not exist
           $('story[source]').parent().parent().addClass('error');
         }
     });
   }else{
      //~ url is not valid, do something
      $('story[source]').parent().parent().addClass('error');
   }
});

function url_validation_check(url){ 
   # this function is a slightly modification of this one: http://bit.ly/14IJ4h0
   return /((https?|ftp):\/\/(\w+:{0,1}\w*@)?(\S+)|)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!-\/]))?/.test(url); 
} 
hamedrnik commented 11 years ago

That's a good idea and we'll handle it with your solution or better one. Anyway thanks for your contribute ;)