Created
October 30, 2015 18:02
-
-
Save jdowd7/f4a439903c202f404286 to your computer and use it in GitHub Desktop.
Checks if a string contains a URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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