• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MjQ0

Related Code
Email validation with Regular Expression
Generic Entity Framework 4.0 Base Repository
ForEach Extension method for IEnumerable<T>
Invoke Extension Method
Use of Extension methods to hide "infrastructure code"
Unity Service Locator for ASP.NET MVC 3.0 Beta 1
Null Dot "Operator" Extension Method
Generic Entity Framework 4.0 Base Repository with Paging
Generic linq-to-sql repository
WCF RIA Services Unity 2.0 DomainService Factory
UnitOfWork Action filter for ASP.NET MVC and nHibernate
A Default Entity Framework 4.0 ObjectContext Factory
Make sure the web.config pages/namespace can be used together with ASP.NET MVC 3 P1 and Razor
Custom C# exception class
Reflection dynamically get private fields or properties
Extension to the HttpClient
Method to hash passwords
Number of sealed / unsealed types in the framework

Url Validation with Regular Expression

A helper method to validate if a URL is valid

0
832 0 0 0 0 1

public static class UrlValidation
{
    public static bool IsValidUrl(string value)
    {
        StringValidation.IsRequired(value, "Validation.IsUrl's value can't be null or empty");

        string strRegex = "^(https?://)" +
                        "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" + //user@ 
                        @"(([0-9]{1,3}\.){3}[0-9]{1,3}" + // IP- 199.194.52.184 
                        "|" + // allows either IP or domain 
                        @"([0-9a-z_!~*'()-]+\.)*" + // tertiary domain(s)- www. 
                        @"([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." + // second level domain 
                        "[a-z]{2,6})" + // first level domain- .com or .museum 
                        "(:[0-9]{1,4})?" + // port number- :80 
                        "((/?)|" + // a slash isn't required if there is no file name 
                        "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";

        Regex re = new Regex(strRegex);

        return re.IsMatch(value);
    }
}

Share: twitter | facebook   Action: used | fork | flag

.net

Mark '.net' tag as 'like'

Mark '.net' tag as 'ignore'

c#

Mark 'c#' tag as 'like'

Mark 'c#' tag as 'ignore'

regular-expression

Mark 'regular-expression' tag as 'like'

Mark 'regular-expression' tag as 'ignore'


 @fredrikn "I'm the master"
3.11k
September 11, 2010 7:10 PM

Fork

 Url Validation with Regular Expression -  @fredrikn Saturday 11, 2010 7:10 PM


0 Feedback


You must log in before you can give any feedback


1 Discussion(s)

Newest Oldest
0

The problem with using Regex is that you are essentially embedding another language into a string object. This means that you won't get any usable feedback of any errors in that string/language. So in my mind this should only be used when there is no other option.

Therefor I would prefere the existing Uri.IsWellFormedUriString method when possible.

link | flag  | Reply

 @jesperpalm "Code Contributor"
156
Sunday 12, 2010 1:38 PM


You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010