Quantcast
Read WebProNews
With Friends!

Validating a URL with Regular Expressions

Get the WebProNews Newsletter:

I had to build web form that took user input from standard ASP.NET input controls. In one of the text boxes the user must enter a valid URL, so I had to make some validation logic.

But first of all, I had to find out what kind of URL’s we would accept as being valid. These are the rules we decided upon:

  • The protocol must be http or https
  • Sub domains are allowed
  • Query strings are allowed
  • Based on those rules, I wrote this regular expression:

    (http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?

    It is used in a RegularExpressionValidator control on the web form and on a business object in C#.

    <asp:RegularExpressionValidator runat="Server"

      ControlToValidate="txtUrl"

      ValidationExpression="(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"

      ErrorMessage="Please enter a valid URL"

      Display="Dynamic" />

    Here is the server-side validator method used by the business object:

    using System.Text.RegularExpressions;

    private bool IsUrlValid(string url)

    {

      return Regex.IsMatch(url, @"(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");

    }

    You can add more protocols to the expression easily. Just add them to the beginning of the regular expression:

    (http|https|ftp|mailto)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?

    You can also allow every thinkable protocol containing at least 3 characters by doing this:

    ([a-zA-Z]{3,})://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?

    Comments

    Tag:

    Add to Del.icio.us | Digg | Yahoo! My Web | Furl

    Bookmark WebProNews:

    Mads Kristensen currently works as a Senior Developer at Traceworks located
    in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
    2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
    web services in his daily work as well. A true .NET developer with great passion for the simple solution.

    http://www.madskristensen.dk/

    About Mads Kristensen
    Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

    http://www.madskristensen.dk/
    Top Rated White Papers and Resources
    There are 4 Comments. Add Yours.
    1. Like (0) Dislike (0)
      Guest

      this is a good article

      Reply
    2. Like (0) Dislike (0)
      Guest

      this logic is not working for any illegal URL. It always returns true.

      Reply
    3. Like (0) Dislike (0)
      Guest

      Comment: *

      Reply

    What do you think? Respond.

    Your email address will not be published. Required fields are marked *

    *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>