1

I use Asp.net 4 and C# Web Forms.

In my Web Application Users can add URLS using a TextBox.

I need to make sure that every value inserted has a syntactically correct URL format (I do not need to check if the URL really exists).

So as first rule I would like check using a CustomValidator Control if the Input inserted by the User has the value string "http://" at the beginning.

My questions?

  • Are you able to provide me a RegEx to add to my CustomValidator Control that will let to pass only string beginning with "http://"?

  • Do you have any other rule using RegEx to suggest me?

  • What is you best practice to detect detect syntactically correct URL?

Thanks for your help

GibboK
  • 71,848
  • 143
  • 435
  • 658

3 Answers3

9

An easier approach in many ways, and more flexible to later changes, is to just try it and see:

public static bool IsValidHttpUri(string uriString)
{
  Uri test = null;
  return Uri.TryCreate(uriString, UriKind.Absolute, out test) && test.Scheme == "http";
)

Using Uri.IsWellFormedUriString is easier still, but doesn't check your requirement that the URI must be an HTTP one.

Edit: Oh, whether this considers IRIs valid or not depends on configuration, see the section on "International Resource Identifier Support" at http://msdn.microsoft.com/en-us/library/system.uri.aspx As a rule, whether you want them to be considered valid or not will match this configuration setting anyway, so this is actually a benefit in most cases.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
  • John thanks, I really like your approach. Do you know why try TryCreate return TRUE for URI like "mail:foo"?? Here on msdn schema support by Uri http://msdn.microsoft.com/en-us/library/system.uri.scheme.aspx but I cannot find an explanation... maybe a Bug?? – GibboK Mar 21 '11 at 12:03
  • Uri supports any scheme, including those not yet invented. You may after all implement a scheme later than .NET being built, so the Uri class needs to be flexible enough for that. Note that the page you link to says "This property does not indicate that the scheme used to initialize the Uri instance was recognized." and that the table are just "*some* possible values". – Jon Hanna Mar 21 '11 at 12:28
  • I am not convinced of this solution. The `Uri` class happily accepts URIs such as `http://------/`, which other software (Semantic Web-related) then chokes on, claiming that is a malformed URI. – O. R. Mapper May 16 '14 at 13:57
  • I realized meanwhile that the other software uses an older URI syntax than what is currently considered valid - so I would like to specify that this solution only works when the target software that needs to process the URI uses the same URI syntax version as does .NET's `Uri` class. – O. R. Mapper May 17 '14 at 09:03
  • @O.R.Mapper I'm not in a position to try now, but I note that the URI example you give is a valid URI but not a valid IRI (appropriate, RDF and other Sem-Web stuff use IRIs being more modern than the earlier web stuff that predate them). Do the `` and `` settings as per the page I linked to in my answer fix your problem? As documented, I would say that `Uri.TryCreate` **should** accept that URI without those settings and **should not** with them. – Jon Hanna May 17 '14 at 10:53
0
Regex regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);
user623879
  • 4,066
  • 9
  • 38
  • 53
  • @stema: Really? Have you [tried](http://regexhero.net/) it? – O. R. Mapper May 17 '14 at 09:06
  • @O.R.Mapper, I don't need to. This is for sure not the regex my comment was meant for. This regex is far more wrong. – stema May 19 '14 at 06:18
  • @stema: I'm confused. According to what SO displays, this answer has not been edited. – O. R. Mapper May 19 '14 at 06:41
  • 1
    @O.R.Mapper, I know. Within 5 minutes the edits are not shown as edits. But my first comment was after those 5 minutes. I am sorry but I can't remember what happened 3 years ago. But I am sure I can see in a pattern when it requires to start with "www". The current pattern would even match "http://+" as a valid URL. – stema May 19 '14 at 07:01
0

In my (limited) experience regex wastes a lot of resources for such a simple task (string starts with http:// or https://)

You might want to consider checking if the url has 'illegal' characters. about urlencoding

PLane
  • 186
  • 1
  • 7