Skip to content

Instantly share code, notes, and snippets.

@jdowd7
Created October 30, 2015 18:02
Show Gist options
  • Save jdowd7/f4a439903c202f404286 to your computer and use it in GitHub Desktop.
Save jdowd7/f4a439903c202f404286 to your computer and use it in GitHub Desktop.
Checks if a string contains a URL
public static ValidationResult ContainsUrl(string sampleStr)
{
if(Uri.IsWellFormedUriString(sampleStr, UriKind.Absolute))
{
return new ValidationResult("Message Contains a URL.");
}
Regex urlRx = new Regex(@"(?<url>(http:[/][/]|www.)([a-z]|[A-Z]|[0-9]|[/.]|[~])*)", RegexOptions.IgnoreCase);
MatchCollection matches = urlRx.Matches(sampleStr);
if(matches.Count > 0)
{
return new ValidationResult("Message Contains a URL.");
}
return ValidationResult.Success;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment