Search

Validating a URL with Regular Expressions

0 views

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: It is used in a RegularExpressionValidator control on the web form and on a business object in C#. <asp:RegularExpressionValidator runat="Server"   ControlToValidate="txtUrl"   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) { } You can add more protocols to the expression easily. Just add them to the beginning of the regular expression: You can also allow every thinkable protocol containing at least 3 characters by doing this:

    Suggest a Correction

    Found an error or have a suggestion? Let us know and we'll review it.

Share this article

Comments (0)

Please sign in to leave a comment.

No comments yet. Be the first to comment!