2

I have searched a lot but didn't get the answer. How to validate URLs like www.google.com and http://www.google.com using regular expressions? Thanks in advance.

Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476
manoj
  • 515
  • 9
  • 28

2 Answers2

2

You can use a function to test valid url as:

function validateUrl()   // return true or false.
{
    var urlregex = new RegExp(
          "^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)");
    return urlregex.test(textval);
}
svlasov
  • 8,802
  • 2
  • 36
  • 38
Umesh Sehta
  • 1,599
  • 9
  • 22
1

You can also use this one, that does not depend on the string start/end:

(\b((?:https?|ftp):\/\/|www\.)([0-9A-Za-z]+\.?)+\b)

See example here.

Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476