1

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
  • 607,720
  • 39
  • 448
  • 563
manoj
  • 505
  • 9
  • 29

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
  • 9,923
  • 2
  • 38
  • 39
Umesh Sehta
  • 10,555
  • 5
  • 39
  • 68
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
  • 607,720
  • 39
  • 448
  • 563