S
shapper
Hello,
I need to validate an url in my C# code.
Does anyone knows a reliable Regex to validate a Url?
Thanks,
Miguel
I need to validate an url in my C# code.
Does anyone knows a reliable Regex to validate a Url?
Thanks,
Miguel
And be careful, since the most of the examples floating around on the
web only show how to validate a subset of valid URLs, which may or may
not be acceptable depending on your needs.
And be careful, since the most of the examples floating around on the
web only show how to validate a subset of valid URLs, which may or may
not be acceptable depending on your needs.
I am using the following:
Boolean valid = Regex.IsMatch(context.PropertyValue, @"((?<=\()
[A-Za-z][A-Za-z0-9\+\.\-]*[A-Za-z0-9\.\-_~:/\?#\[\]@!\$&'\(\)\*\
+,;=]|%[A-Fa-f0-9]{2})+(?=\)))|([A-Za-z][A-Za-z0-9\+\.\-]*[A-Za-
z0-9\.\-_~:/\?#\[\]@!\$&'\(\)\*\+,;=]|%[A-Fa-f0-9]{2})+)");
Based on the following Regex:
http://blog.dieweltistgarnichtso.net/constructing-a-regular-expression-that-matches-uris
Not sure if it is the best one but it seems ok.
Basically in my case I am adding Slides (Images) to site in a CMS.
Each image can have a link to some site.
I want to be sure that the url inserted has a valid format.
Thanks,
Miguel
* shapper wrote, On 10-9-2009 17:15:
I am using the following:
Boolean valid = Regex.IsMatch(context.PropertyValue, @"((?<=\()
[A-Za-z][A-Za-z0-9\+\.\-]*[A-Za-z0-9\.\-_~:/\?#\[\]@!\$&'\(\)\*\
+,;=]|%[A-Fa-f0-9]{2})+(?=\)))|([A-Za-z][A-Za-z0-9\+\.\-]*[A-Za-
z0-9\.\-_~:/\?#\[\]@!\$&'\(\)\*\+,;=]|%[A-Fa-f0-9]{2})+)");Based on the following Regex:
http://blog.dieweltistgarnichtso.net/constructing-a-regular-expressio...Not sure if it is the best one but it seems ok.Basically in my case I am adding Slides (Images) to site in a CMS.
Each image can have a link to some site.
I want to be sure that the url inserted has a valid format.Thanks,
Miguel
If you want to make sure you can open the image and that the url
actually exists, the best way is to use the URI class and actually try
to open the resource using WebClient.
The definition of a URL/URI is so broad and can contain so many
protocols and subsequent URI scheme's that a regex probably only works
on the most standard cases.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.