I am allowing my users to insert a website url.
As you all know, some people are completely useless, typing whatever to the input, without any check.
I want to do a test on the user inputted url, to check if it is valid, and if not, repair it automatically using javascript, before displaying it to the public.
Example user submitted urls:
- http://google.com
- www.google.com
- google.com
- http//google.com
- htp:/google.com
- ++++++++
var web = user.url;
var urlCheck = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
if(web){
if(urlCheck.test(web) === false) {
web = "http://" + web;
}
$('div.Link').html("<a href=\"javascript:void(0);\" onclick=\"externalLink('" + web + "');\">" + web + "</a>");
}
Suggestions on for an even better check/test to avoid problems?