.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Code Snippets >> Validations >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

How to Validate URL

Posted By:Ravi Ranjan Kumar       Posted Date: October 31, 2012    Points: 40    Category: Validations    URL: http://www.dotnetspark.com  

This code snippet enable you to understand how to Validate URL enter by user.

You can use given function to validate URL entered by user.
public static bool IsUrl(string url)
    string pattern= "^(https?://)"
    + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //user@
    + @"(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP-
    + "|" // 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 rx = new Regex(pattern);

    if (rx.IsMatch(url))
      return (true); // if URL is validate 
      return (false); // if URL is not validate.


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend