/// <summary> /// Validates a string compared to a set of regex patterns /// <param name="data">String to validate</param> /// <param name="regexPattern">Regex pattern to validate with</param> /// </summary> /// /// <remarks>More Info: http://gist.github.com/2391792 </remarks> //Note: I've edited the regex pattern for email address to allow the . character before the @ and to allow numbers at the end for domains like 6x.to ~Pete public static bool validateInformation(string data, RegexPattern regexPattern) { string pattern = string.Empty; switch (regexPattern) { case RegexPattern.NameString: pattern = "^[A-Za-z -]+$"; break; case RegexPattern.NumericalString: pattern = "^[0-9]+$"; break; case RegexPattern.EmailString: pattern = "^[0-9A-Za-z.]+[@][0-9A-Za-z]+[.][A-Za-z0-9.]+$"; break; case RegexPattern.PriceString: pattern = @"^[0-9]+\.[0-9]+$"; break; case RegexPattern.PhoneString: pattern = "[+]?[0-9]{5,6} ?[0-9]{3} ?[0-9]{3}"; break; } return new Regex(pattern).IsMatch(data); }
public static string GetRegexValue(RegexPattern type) { string result = String.Empty; switch (type) { case RegexPattern.None: result = String.Empty; break; case RegexPattern.NUMBER: result = NUMBER; break; case RegexPattern.ALPHA: result = ALPHA; break; case RegexPattern.ALPHA_NUMERIC: result = ALPHA_NUMERIC; break; case RegexPattern.ALPHA_UNDERLINE: result = ALPHA_UNDERLINE; break; case RegexPattern.ALPHA_NUMERIC_UNDERLINE: result = ALPHA_NUMERIC_UNDERLINE; break; case RegexPattern.ALPHA_LOWER_CASE: result = ALPHA_LOWER_CASE; break; case RegexPattern.ALPHA_UPPER_CASE: result = ALPHA_UPPER_CASE; break; case RegexPattern.EMAIL: result = EMAIL; break; case RegexPattern.URL: result = URL; break; case RegexPattern.POSTAL_CODE: result = POSTAL_CODE; break; case RegexPattern.IP_ADDRESS: result = IP_ADDRESS; break; case RegexPattern.IDENTITY_CARD: result = IDENTITY_CARD; break; } return result; }