RegexInspection.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using UnityEngine;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System;
  8. namespace LoyalSoftSDK
  9. {
  10. public class RegexInspection
  11. {
  12. public static string AccountInspection(string account)
  13. {
  14. if (string.IsNullOrEmpty(account))
  15. {
  16. return "手机号不能为空";
  17. }
  18. Regex rx = new Regex(@"^0{0,1}[0-9]{11}$");
  19. if (!rx.IsMatch(account)) //不匹配
  20. {
  21. return "手机号错误,请输入正确手机号!";
  22. }
  23. return "";
  24. }
  25. public static string PassWordInspection(string passWord)
  26. {
  27. if (string.IsNullOrEmpty(passWord))
  28. {
  29. return "密码不能为空";
  30. }
  31. Regex rx = new Regex(@"\S{6,20}");
  32. if (!rx.IsMatch(passWord)) //不匹配
  33. {
  34. return "密码格式错误,请输入6~20位非空字符!";
  35. }
  36. return null;
  37. }
  38. public static string OldPassWordInspection(string passWord)
  39. {
  40. if (string.IsNullOrEmpty(passWord))
  41. {
  42. return "旧密码不能为空";
  43. }
  44. Regex rx = new Regex(@"\S{6,20}");
  45. if (!rx.IsMatch(passWord)) //不匹配
  46. {
  47. return "旧密码格式错误,请输入6~20位非空字符!";
  48. }
  49. return null;
  50. }
  51. public static string CodeInspection(string code)
  52. {
  53. if (string.IsNullOrEmpty(code))
  54. {
  55. return "验证码不能为空";
  56. }
  57. Regex rx = new Regex(@"[0-9]{6}");
  58. if (!rx.IsMatch(code)) //不匹配
  59. {
  60. return "验证码错误!";
  61. }
  62. return null;
  63. }
  64. public static string NameInspection(string realName)
  65. {
  66. if (string.IsNullOrEmpty(realName))
  67. {
  68. return "姓名不能为空";
  69. }
  70. Regex rx = new Regex(@"^([\u4e00-\u9fa5]{1,20}|[a-zA-Z\.\s]{1,20})$");
  71. if (!rx.IsMatch(realName)) //不匹配
  72. {
  73. return "姓名错误!";
  74. }
  75. return null;
  76. }
  77. public static string IDCardInspection(string code)
  78. {
  79. if (string.IsNullOrEmpty(code))
  80. {
  81. return "身份证号不能为空";
  82. }
  83. // Regex rx = new Regex(@"(^\d{18}$)|(^\d{15}$)");
  84. //if (!rx.IsMatch(code)) //不匹配
  85. if(!IDCardValidation.CheckIDCard(code))
  86. {
  87. return "身份证号错误!";
  88. }
  89. return null;
  90. }
  91. public static string GetMD5Hash(string editStr)
  92. {
  93. MD5 md5Hash = MD5.Create();
  94. byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(editStr));
  95. //Create a new StringBuilder to collect the bytes and create a string.
  96. StringBuilder builder = new StringBuilder();
  97. //Loop through each byte of the hashed data and format each one as a hexadecimal strings.
  98. for (int cnt = 0; cnt < data.Length; cnt++)
  99. {
  100. builder.Append(data[cnt].ToString("x2"));
  101. }
  102. //Return the hexadecimal string
  103. return builder.ToString();
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// 验证身份证号码类
  109. /// </summary>
  110. public class IDCardValidation
  111. {
  112. /// <summary>
  113. /// 验证身份证合理性
  114. /// </summary>
  115. /// <param name="Id"></param>
  116. /// <returns></returns>
  117. static public bool CheckIDCard(string idNumber)
  118. {
  119. if (idNumber.Length == 18)
  120. {
  121. bool check = CheckIDCard18(idNumber);
  122. return check;
  123. }
  124. else if (idNumber.Length == 15)
  125. {
  126. bool check = CheckIDCard15(idNumber);
  127. return check;
  128. }
  129. else
  130. {
  131. return false;
  132. }
  133. }
  134. /// <summary>
  135. /// 18位身份证号码验证
  136. /// </summary>
  137. static private bool CheckIDCard18(string idNumber)
  138. {
  139. long n = 0;
  140. if (long.TryParse(idNumber.Remove(17), out n) == false
  141. || n < Math.Pow(10, 16) || long.TryParse(idNumber.Replace('x', '0').Replace('X', '0'), out n) == false)
  142. {
  143. return false;//数字验证
  144. }
  145. string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
  146. if (address.IndexOf(idNumber.Remove(2)) == -1)
  147. {
  148. return false;//省份验证
  149. }
  150. string birth = idNumber.Substring(6, 8).Insert(6, "-").Insert(4, "-");
  151. DateTime time = new DateTime();
  152. if (DateTime.TryParse(birth, out time) == false)
  153. {
  154. return false;//生日验证
  155. }
  156. string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
  157. string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
  158. char[] Ai = idNumber.Remove(17).ToCharArray();
  159. int sum = 0;
  160. for (int i = 0; i < 17; i++)
  161. {
  162. sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
  163. }
  164. int y = -1;
  165. Math.DivRem(sum, 11, out y);
  166. if (arrVarifyCode[y] != idNumber.Substring(17, 1).ToLower())
  167. {
  168. return false;//校验码验证
  169. }
  170. return true;//符合GB11643-1999标准
  171. }
  172. /// <summary>
  173. /// 15位身份证号码验证
  174. /// </summary>
  175. static private bool CheckIDCard15(string idNumber)
  176. {
  177. long n = 0;
  178. if (long.TryParse(idNumber, out n) == false || n < Math.Pow(10, 14))
  179. {
  180. return false;//数字验证
  181. }
  182. string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
  183. if (address.IndexOf(idNumber.Remove(2)) == -1)
  184. {
  185. return false;//省份验证
  186. }
  187. string birth = idNumber.Substring(6, 6).Insert(4, "-").Insert(2, "-");
  188. DateTime time = new DateTime();
  189. if (DateTime.TryParse(birth, out time) == false)
  190. {
  191. return false;//生日验证
  192. }
  193. return true;
  194. }
  195. }