Is it safe to validate a URL with a regexp?

Posted by Vilx- on Stack Overflow See other posts from Stack Overflow or by Vilx-
Published on 2010-06-17T00:31:23Z Indexed on 2010/06/17 0:42 UTC
Read the original article Hit count: 200

Filed under:
|
|
|

In my web app I've got a form field where the user can enter an URL. I'm already doing some preliminary client-side validation and I was wondering if I could use a regexp to validate if the entered string is a valid URL. So, two questions:

  1. Is it safe to do this with a regexp? A URL is a complex beast, and just like you shouldn't use a regexp for parsing HTML, I'm worried that it might be unsuitable for a URL as well.
  2. If it can be done, what would be a good regexp for the task? (I know that Google turns up countless regexps, but I'm worried about their quality).

My goal is to prevent a situation where the URL appears in the web page and is unusable by the browser.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about regex