3 Replies - 1617 Views - Last Post: 30 April 2012 - 11:58 AM

#1 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Validate URL with Regular Expressions

Posted 18 April 2009 - 09:53 AM

Description: Need a reference to the System.Text.RegularExpressions NamespaceThis is a snippet for validating a url uytilizing regular expressions. This pattern allows for https, http, and ftp
/// 
/// method for validating a url with regular expressions
/// 
/// url we're validating
/// true if valid, otherwise false
public static bool isValidUrl(ref string url)
{
    string pattern = @"^(http|https|ftp)://[a-zA-Z0-9-.]+.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9-._?,'/\+&%$#=~])*[^.,)(s]$";
    Regex reg = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
    return reg.IsMatch(url);
}


Is This A Good Question/Topic? 0
  • +

Replies To: Validate URL with Regular Expressions

#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 187
  • View blog
  • Posts: 4,673
  • Joined: 26-November 04

Re: Validate URL with Regular Expressions

Posted 25 April 2011 - 02:47 PM

Fails for string of the form: "www.domain.com////////"
Was This Post Helpful? 0
  • +
  • -

#3 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 187
  • View blog
  • Posts: 4,673
  • Joined: 26-November 04

Re: Validate URL with Regular Expressions

Posted 25 April 2011 - 02:51 PM

My bad. Had no idea that URLs terminating with multiple consecutive slashes are valid.
Was This Post Helpful? 0
  • +
  • -

#4 taozen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-April 12

Re: Validate URL with Regular Expressions

Posted 30 April 2012 - 11:58 AM

Fails with 2 letter domains, like http://coop.dk - only http://coop.dk/ works
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1