I had an text field where user has to place an url. I need to validate the url whether format is valid.I need to write an reg-exp to find below invalid urls.
http://www.google.com//test/index.html //Because of double slash after host name
http:/www.google.com/test/index.html //Missing double slash for protocol
I tried below code which is working for second case but not for first case
function test(url) {
var exp=/^(https?:\/\/)?((([a-z\d]([a-z\d-]*[a-z\d])*)\.)+[a-z]{2,}|((\d{1,3}\.){3}\d{1,3}))(\:\d+)?(\/[-a-z\d%_.~+]*)*(\?[;&a-z\d%_.~+=-]*)?(\#[-a-z\d_]*)?$/;
return exp.test(url);
}
var firstCase="http://www.google.com//test/index.html";
alert(test(firstCase));
var secondCase = "http:/www.google.com/test/index.html";
alert(test(secondCase ));
var thirdCase="http://www.google.com/test//index.html";
alert(test(thirdCase));