Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if url is valid or not [duplicate]

I tried to check if url is valid or invalid. the checks of 7,8 returns wrong outputs.

alert('1: ' + learnRegExp('http://www.google-com.123.com')); // true
alert('2: ' + learnRegExp('http://www.google-com.123')); // false
alert('3: ' + learnRegExp('https://www.google-com.com')); // true
alert('4: ' + learnRegExp('http://google-com.com')); // true
alert('5: ' + learnRegExp('http://google.com')); //true
alert('6: ' + learnRegExp('google.com')); //true
alert('7: ' + learnRegExp('ww.google.com')); //false -> it returns true
alert('8: ' + learnRegExp('www.google.co.il')); //true -> it returns false
alert('9: ' + learnRegExp('http://ww.google.co.il')); //false
alert('10: ' + learnRegExp('https://ww.google.co.il')); //false

function learnRegExp(){
    return /((ftp|https?):\/\/)?(www\.)?[a-z0-9\-\.]{3,}\.[a-z]{3}$/
    .test(learnRegExp.arguments[0]);
}

please help me to solve it.

any help appreciated!

like image 513
Alon Shmiel Avatar asked Jul 18 '13 14:07

Alon Shmiel


People also ask

How do you check the URL is valid or not?

You can use the URLConstructor to check if a string is a valid URL. URLConstructor ( new URL(url) ) returns a newly created URL object defined by the URL parameters. A JavaScript TypeError exception is thrown if the given URL is not valid.

What is a URL validator?

Link validation pings the destination of a URL and tests for errors. This helps avoid broken and invalid links in your published document, and is especially useful for bloggers.

How do you check if a URL is valid or not in Python?

Using the validators package The URL validation function is available in the root of the module and will return True if the string is a valid URL, otherwise it returns an instance of ValidationFailure , which is a bit weird but not a deal breaker.

How can I check if an android URL is valid?

Use URLUtil to validate the URL as below. It will return True if URL is valid and false if URL is invalid. Save this answer.


1 Answers

Try this one:

 function learnRegExp(s) {    
      var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
      return regexp.test(s);    
 }
like image 164
Ishan Jain Avatar answered Sep 24 '22 01:09

Ishan Jain