Validating url in javascript

', true]); tests.push([' false]); tests.push([' false]); tests.push([' true]); tests.push([' true]); tests.push(['https://medium.com/@techytimo', true]); tests.push([' true]); tests.push(['localhost', true]); tests.push(['localhost:8080', true]); tests.push(['localhost:65536', true]); tests.push(['localhost:80000', false]); tests.push(['magnet:?xt=urn:btih:123', true]); for (let i = 0; i , without the protocol or any dot in the hostname.

Hanging like that makes that code unusable in any serious application.Replicates the functionality of Richard Sonnen [email protected] module : code here into a nodejs module. All functions return an untainted value if the test passes, and undef if it fails. (copied from original perl module) This module collects common URI validation routines to make input validation, and untainting easier and more readable.sa=i&rct=j&q=&esrc=s&source=images&cd=&docid=n Iv5rk2Gy P3h XM&tbnid=isi Ok Me3n Ctex M:&ved=0CAUQj Rw&url= ZXU_2f GKb Ms QTf4YLg AQ&bvm=bv.65177938,d.a Wc&psig=AFQj CNEp Bf Knal9k U7Zu4n7Rn Et2ner N4g&ust=1398298682009707"; console.log(is Valid URL(test Case2)); // return true var test Case3 = "https://sdfasd"; console.log(is Valid URL(test Case3)); // return false var test Case4 = "dfdsfdsfdfdsfsdfs"; console.log(is Valid URL(test Case4)); // return false var test Case5 = "magnet:?xt=urn:btih:123"; console.log(is Valid URL(test Case5)); // return false var test Case6 = "https://stackoverflow.com/"; console.log(is Valid URL(test Case6)); // return true var test Case7 = "https://w"; console.log(is Valid URL(test Case7)); // return false var test Case8 = "https://sdfasdp.ppppppppppp"; console.log(is Valid URL(test Case8)); // return falsevar elm; function is Valid URL(u) console.log(is Valid URL(' console.log(is Valid URL('//google.com')); console.log(is Valid URL('google.com')); console.log(is Valid URL('localhost:8000')); (I don't have reps to comment on Valid URL example; hence post this as an answer.) While use of protocol relative URLs is not encouraged (The Protocol-relative URL), they do get employed sometimes.

Leave a Reply