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.

2
  • What do you mean by validate an URL? Check if it starts with "http://", or "www": ^(?:http|www). Have you tried anything? Commented Apr 1, 2015 at 10:43
  • it could be starting from both "www" or "http://" Commented Apr 1, 2015 at 10:45

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

your solution not working at all. see this fiddle jsfiddle.net/KJW4E/1284
jsfiddle.net/KJW4E/1286 this also not working... returning false
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.