Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Text.RegularExpressions;
  4.  
  5. public class RegexUtilities
  6. {
  7. bool invalid = false;
  8.  
  9. public bool IsValidEmail(string strIn)
  10. {
  11. invalid = false;
  12. if (String.IsNullOrEmpty(strIn))
  13. return false;
  14.  
  15. // Use IdnMapping class to convert Unicode domain names. 
  16. try {
  17. strIn = Regex.Replace(strIn, @"(@)(.+)$", this.DomainMapper,
  18. RegexOptions.None, TimeSpan.FromMilliseconds(200));
  19. }
  20. catch (RegexMatchTimeoutException) {
  21. return false;
  22. }
  23.  
  24. if (invalid)
  25. return false;
  26.  
  27. // Return true if strIn is in valid e-mail format. 
  28. try {
  29. return Regex.IsMatch(strIn,
  30. @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
  31. @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
  32. RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
  33. }
  34. catch (RegexMatchTimeoutException) {
  35. return false;
  36. }
  37. }
  38.  
  39. private string DomainMapper(Match match)
  40. {
  41. // IdnMapping class with default property values.
  42. IdnMapping idn = new IdnMapping();
  43.  
  44. string domainName = match.Groups[2].Value;
  45. try {
  46. domainName = idn.GetAscii(domainName);
  47. }
  48. catch (ArgumentException) {
  49. invalid = true;
  50. }
  51. return match.Groups[1].Value + domainName;
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement