RegexUtilities.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Globalization;
  3. using System.Text.RegularExpressions;
  4. using UnityEngine;
  5. public class RegexUtilities
  6. {
  7. bool invalid = false;
  8. public bool IsValidEmail(string strIn)
  9. {
  10. invalid = false;
  11. if (String.IsNullOrEmpty(strIn))
  12. return false;
  13. // Use IdnMapping class to convert Unicode domain names.
  14. try
  15. {
  16. strIn = Regex.Replace(strIn, @"(@)(.+)$", this.DomainMapper,
  17. RegexOptions.None);
  18. }
  19. catch (RegexMatchTimeoutException)
  20. {
  21. return false;
  22. }
  23. if (invalid)
  24. return false;
  25. // Return true if strIn is in valid e-mail format.
  26. try
  27. {
  28. return Regex.IsMatch(strIn,
  29. @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
  30. @"(?(\[)(\[(\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]))$",
  31. RegexOptions.IgnoreCase);
  32. }
  33. catch (RegexMatchTimeoutException)
  34. {
  35. return false;
  36. }
  37. }
  38. public bool IsValidPhone(string Phone)
  39. {
  40. try
  41. {
  42. char[] ar = new char[]{};
  43. string number = "";
  44. switch (LoginManager.Instance.CurStateCountryCodePhone)
  45. {
  46. case LoginManager.StateCountryTelephoneCode.Danmark:
  47. ar = Phone.ToCharArray();
  48. //if (ar[0].ToString() == "7")
  49. //{
  50. // if (ar[1].ToString() == "0" || ar[1].ToString() == "2"
  51. // || ar[1].ToString() == "3" || ar[1].ToString() == "6" || ar[1].ToString() == "9")
  52. {
  53. for (int i = 0; i < ar.Length; i++)
  54. {
  55. if (Char.IsDigit(ar[i]))
  56. {
  57. number += ar[i];
  58. }
  59. }
  60. }
  61. //}
  62. if (number.ToCharArray().Length <= 9 && number.ToCharArray().Length >= 8)
  63. {
  64. // Debug.Log(number);
  65. return true;
  66. }
  67. return false;
  68. break;
  69. case LoginManager.StateCountryTelephoneCode.Ukraine:
  70. ar = Phone.ToCharArray();
  71. //if (ar[0].ToString() == "0" && ar[1].ToString() == "7")
  72. {
  73. //if (ar[2].ToString() == "0" || ar[2].ToString() == "2"
  74. // || ar[2].ToString() == "3" || ar[2].ToString() == "6" || ar[2].ToString() == "9")
  75. {
  76. for (int i = 0; i < ar.Length; i++)
  77. {
  78. if (Char.IsDigit(ar[i]))
  79. {
  80. number += ar[i];
  81. }
  82. }
  83. }
  84. }
  85. if (number.ToCharArray().Length == 10)
  86. {
  87. // Debug.Log(number);
  88. return true;
  89. }
  90. return false;
  91. break;
  92. case LoginManager.StateCountryTelephoneCode.Iceland:
  93. ar = Phone.ToCharArray();
  94. //if (ar[0].ToString() == "0" && ar[1].ToString() == "7")
  95. {
  96. //if (ar[2].ToString() == "0" || ar[2].ToString() == "2"
  97. // || ar[2].ToString() == "3" || ar[2].ToString() == "6" || ar[2].ToString() == "9")
  98. {
  99. for (int i = 0; i < ar.Length; i++)
  100. {
  101. if (Char.IsDigit(ar[i]))
  102. {
  103. number += ar[i];
  104. }
  105. }
  106. }
  107. }
  108. if (number.ToCharArray().Length == 7)
  109. {
  110. // Debug.Log(number);
  111. return true;
  112. }
  113. return false;
  114. break;
  115. }
  116. }
  117. catch (Exception)
  118. {
  119. throw;
  120. }
  121. return false;
  122. }
  123. /* public bool IsValidNumberPhone(string strIn)
  124. {
  125. invalid = false;
  126. if (String.IsNullOrEmpty(strIn))
  127. return false;
  128. if (invalid)
  129. return false;
  130. // Return true if strIn is in valid phone number format.
  131. try
  132. {
  133. Debug.Log(Regex.IsMatch(strIn,
  134. @"^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$",
  135. RegexOptions.IgnoreCase));
  136. return Regex.IsMatch(strIn,
  137. @"^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$",
  138. RegexOptions.IgnoreCase);
  139. }
  140. catch (RegexMatchTimeoutException)
  141. {
  142. return false;
  143. }
  144. }*/
  145. private string DomainMapper(Match match)
  146. {
  147. // IdnMapping class with default property values.
  148. IdnMapping idn = new IdnMapping();
  149. string domainName = match.Groups[2].Value;
  150. try
  151. {
  152. domainName = idn.GetAscii(domainName);
  153. }
  154. catch (ArgumentException)
  155. {
  156. invalid = true;
  157. }
  158. return match.Groups[1].Value + domainName;
  159. }
  160. }
  161. public class RegexMatchTimeoutException : Exception
  162. {
  163. }