1function validURL(str) {
2 var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
3 '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
4 '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
5 '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
6 '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
7 '(\\#[-a-z\\d_]*)?$','i'); // fragment locator
8 return !!pattern.test(str);
9}
1function isValidURL(string) {
2 var res = string.match(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g);
3 return (res !== null)
4};
5
6var testCase1 = "http://en.wikipedia.org/wiki/Procter_&_Gamble";
7
8alert(isValidURL(testCase1));
9
1function isValidHttpUrl(string) {
2 let url;
3
4 try {
5 url = new URL(string);
6 } catch (_) {
7 return false;
8 }
9
10 return url.protocol === "http:" || url.protocol === "https:";
11}
1function isValidHttpUrl(string) {
2 let url;
3
4 try {
5 url = new URL(string);
6 } catch (_) {
7 return false;
8 }
9
10 return url.protocol === "http:" || url.protocol === "https:";
11}
12
1
2filter_var($url, FILTER_VALIDATE_URL);
3
4if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
5 die('Not a valid!!');
6}