-3

Possible Duplicate:
Regular expression for URL validation (in JavaScript)
How to validate url?

I have a textbox in a form where user can enter any website url. i want to check that url, whether it is valid or not through regular expression. it will accept only like this "http://www.google.com" or "www.google.co.in".

I have using a expression like

/((http|https|ftp):\/\/(www|WWW)|(www|WWW))\.[A-Za-z]{3}/ .

it is working but when i enter wwww.google.co.in, it says valid.

can any one help me please.

Community
  • 1
  • 1
Chirayu
  • 4,693
  • 7
  • 28
  • 46
  • Have you seen this ? http://stackoverflow.com/questions/3604718/looking-for-the-url-regular-expression – tryme Dec 29 '11 at 09:42

2 Answers2

0
  • Do a request server side and check for 200 OK. Don't trust the client.
artificialidiot
  • 5,309
  • 29
  • 27
  • So, because you don't trust the client (you shouldn't indeed), you're willing to turn your server-side module into a DDOS relay? That code could just validate the URL's correctness instead of issuing an actual HTTP request on the client's behalf, don't you think? – Frédéric Hamidi Dec 29 '11 at 09:48
  • It depends on your use case, actually. You may want to just check if the domain exists, or you might generate a screenshot, or you may want to fetch an RSS feed... If you are going to use the url, you might as well put in a rate limiter. – artificialidiot Dec 29 '11 at 14:16
0

We needed it in our project a couple of weeks ago, and I think we used this one

function isUrl(s) {
    var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
    return regexp.test(s);
}

UPDATE: I'm still working on it:

(ftp|http|https):\/\/([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\/+@&#;`~=%!]*)(\.\w{2,})?)*\/?
Hons
  • 3,804
  • 3
  • 32
  • 50
  • no it is not working as i want. it doesn't accept www.google.com. it only accept http://www.google.com – Chirayu Dec 29 '11 at 10:08