ASP.NET’s “Internet Url” Regular Expression Validator

Posted by William on Apr 11, 2009

The regular expression validator is a great tool when a fast, robust, content validator is required. Drop it on the page, choose it’s ControlToValidate and give it an expression to match against the specified control’s content. There are even built in expressions to choose from….one of which is “Internet URL”.

The problem with this is that the default Internet URL (see below) has a few shortfalls.
http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?

  1. Http or Https are required, when a lot of internet users don’t even know this protocol exists, as they think every url begins with www.
  2. Some crazy urls (Visit Scotland I’m looking at you!) can have other characters not supported by this expression in them (i.e , or ;) .

So in order to get around these little issues I altered the expression a little (see below).
(http(s)?://)?([\w-]+\.)+[\w-]+(/[\w- ;,./?%&=]*)?
This offers a wider support for URL’s, but there is a small gotcha that you need to be aware of. So what is the gotcha? If the user adds a url without http or https then an anchor with that url applied will break because it will be calling an external url, which requires the protocol; otherwise it is handled as a relative link by your browser. The solution to this is simple. Check for a protocol when saving the value. (note: we can’t possibly know if a url is supposed to be http or https so if a user doesn’t enter a protocol then we will prepend http. If they did enter a protocol then nothing will be prepended)

1
      entity.Url = StringTools.EnsureProtocol(txtUrl.Text, StringTools.Protocol.Http);

Below is my helper method for doing this, but you can easily write the check inline if you wish. Of course, I recommend you always extract your common code into reusable methods, but that’s another discussion!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
///<summary>
///Defines the type of prefix protocol
///</summary>
///<remarks></remarks>
public enum Protocol
{
Http,
Https,
Ftp,
Smtp,
Pop,
Mail
}
 
///<summary>
///Assesses a url and returns a url that is guaranteed to begin with the specified protocol
///</summary>
///<param name="url">The url to be checked for the protocol</param>
///<returns>A url that is guaranteed to begin with the specified protocol</returns>
///<remarks></remarks>
public static string EnsureProtocol(string url, Protocol protocol)
{
string output = url;
 
if (!string.IsNullOrEmpty(output) && !output.StartsWith(protocol + "://", StringComparison.OrdinalIgnoreCase))
output = string.Format("{0}://{1}", protocol.ToString().ToLower(), url);
 
return output;
}

Happy Coding!