纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

374 lines
12 KiB

1 year ago
  1. using System.Text;
  2. using System.Text.RegularExpressions;
  3. namespace NFine.Code
  4. {
  5. public class Validate
  6. {
  7. private static Regex RegNumber = new Regex("^[0-9]+$");
  8. private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
  9. private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
  10. private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
  11. private static Regex RegEmail = new Regex(@"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样
  12. private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
  13. #region 用户名密码格式
  14. /// <summary>
  15. /// 返回字符串真实长度, 1个汉字长度为2
  16. /// </summary>
  17. /// <returns>字符长度</returns>
  18. public static int GetStringLength(string stringValue)
  19. {
  20. return Encoding.Default.GetBytes(stringValue).Length;
  21. }
  22. /// <summary>
  23. /// 检测用户名格式是否有效
  24. /// </summary>
  25. /// <param name="userName"></param>
  26. /// <returns></returns>
  27. public static bool IsValidUserName(string userName)
  28. {
  29. int userNameLength = GetStringLength(userName);
  30. if (userNameLength >= 4 && userNameLength <= 20 && Regex.IsMatch(userName, @"^([\u4e00-\u9fa5A-Za-z_0-9]{0,})$"))
  31. { // 判断用户名的长度(4-20个字符)及内容(只能是汉字、字母、下划线、数字)是否合法
  32. return true;
  33. }
  34. else
  35. {
  36. return false;
  37. }
  38. }
  39. /// <summary>
  40. /// 密码有效性
  41. /// </summary>
  42. /// <param name="password"></param>
  43. /// <returns></returns>
  44. public static bool IsValidPassword(string password)
  45. {
  46. return Regex.IsMatch(password, @"^[A-Za-z_0-9]{6,16}$");
  47. }
  48. #endregion
  49. #region 数字字符串检查
  50. /// <summary>
  51. /// int有效性
  52. /// </summary>
  53. /// <param name="val"></param>
  54. /// <returns></returns>
  55. static public bool IsValidInt(string val)
  56. {
  57. return Regex.IsMatch(val, @"^[1-9]\d*\.?[0]*$");
  58. }
  59. /// <summary>
  60. /// 是否数字字符串
  61. /// </summary>
  62. /// <param name="inputData">输入字符串</param>
  63. /// <returns></returns>
  64. public static bool IsNumber(string inputData)
  65. {
  66. Match m = RegNumber.Match(inputData);
  67. return m.Success;
  68. }
  69. /// <summary>
  70. /// 是否数字字符串 可带正负号
  71. /// </summary>
  72. /// <param name="inputData">输入字符串</param>
  73. /// <returns></returns>
  74. public static bool IsNumberSign(string inputData)
  75. {
  76. Match m = RegNumberSign.Match(inputData);
  77. return m.Success;
  78. }
  79. /// <summary>
  80. /// 是否是浮点数
  81. /// </summary>
  82. /// <param name="inputData">输入字符串</param>
  83. /// <returns></returns>
  84. public static bool IsDecimal(string inputData)
  85. {
  86. Match m = RegDecimal.Match(inputData);
  87. return m.Success;
  88. }
  89. /// <summary>
  90. /// 是否是浮点数 可带正负号
  91. /// </summary>
  92. /// <param name="inputData">输入字符串</param>
  93. /// <returns></returns>
  94. public static bool IsDecimalSign(string inputData)
  95. {
  96. Match m = RegDecimalSign.Match(inputData);
  97. return m.Success;
  98. }
  99. #endregion
  100. #region 中文检测
  101. /// <summary>
  102. /// 检测是否有中文字符
  103. /// </summary>
  104. /// <param name="inputData"></param>
  105. /// <returns></returns>
  106. public static bool IsHasCHZN(string inputData)
  107. {
  108. Match m = RegCHZN.Match(inputData);
  109. return m.Success;
  110. }
  111. /// <summary>
  112. /// 检测含有中文字符串的实际长度
  113. /// </summary>
  114. /// <param name="str">字符串</param>
  115. public static int GetCHZNLength(string inputData)
  116. {
  117. System.Text.ASCIIEncoding n = new System.Text.ASCIIEncoding();
  118. byte[] bytes = n.GetBytes(inputData);
  119. int length = 0; // l 为字符串之实际长度
  120. for (int i = 0; i <= bytes.Length - 1; i++)
  121. {
  122. if (bytes[i] == 63) //判断是否为汉字或全脚符号
  123. {
  124. length++;
  125. }
  126. length++;
  127. }
  128. return length;
  129. }
  130. #endregion
  131. #region 常用格式
  132. /// <summary>
  133. /// 验证身份证是否合法 15 和 18位两种
  134. /// </summary>
  135. /// <param name="idCard">要验证的身份证</param>
  136. public static bool IsIdCard(string idCard)
  137. {
  138. if (string.IsNullOrEmpty(idCard))
  139. {
  140. return false;
  141. }
  142. if (idCard.Length == 15)
  143. {
  144. return Regex.IsMatch(idCard, @"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$");
  145. }
  146. else if (idCard.Length == 18)
  147. {
  148. return Regex.IsMatch(idCard, @"^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[A-Z])$", RegexOptions.IgnoreCase);
  149. }
  150. else
  151. {
  152. return false;
  153. }
  154. }
  155. /// <summary>
  156. /// 是否是邮件地址
  157. /// </summary>
  158. /// <param name="inputData">输入字符串</param>
  159. /// <returns></returns>
  160. public static bool IsEmail(string inputData)
  161. {
  162. Match m = RegEmail.Match(inputData);
  163. return m.Success;
  164. }
  165. /// <summary>
  166. /// 邮编有效性
  167. /// </summary>
  168. /// <param name="zip"></param>
  169. /// <returns></returns>
  170. public static bool IsValidZip(string zip)
  171. {
  172. Regex rx = new Regex(@"^\d{6}$", RegexOptions.None);
  173. Match m = rx.Match(zip);
  174. return m.Success;
  175. }
  176. /// <summary>
  177. /// 固定电话有效性
  178. /// </summary>
  179. /// <param name="phone"></param>
  180. /// <returns></returns>
  181. public static bool IsValidPhone(string phone)
  182. {
  183. Regex rx = new Regex(@"^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$", RegexOptions.None);
  184. Match m = rx.Match(phone);
  185. return m.Success;
  186. }
  187. /// <summary>
  188. /// 手机有效性
  189. /// </summary>
  190. /// <param name="strMobile"></param>
  191. /// <returns></returns>
  192. public static bool IsValidMobile(string mobile)
  193. {
  194. Regex rx = new Regex(@"^(13|15|17|18|19)\d{9}$", RegexOptions.None);
  195. Match m = rx.Match(mobile);
  196. return m.Success;
  197. }
  198. /// <summary>
  199. /// 电话有效性(固话和手机 )
  200. /// </summary>
  201. /// <param name="strVla"></param>
  202. /// <returns></returns>
  203. public static bool IsValidPhoneAndMobile(string number)
  204. {
  205. Regex rx = new Regex(@"^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$|^(13|15)\d{9}$", RegexOptions.None);
  206. Match m = rx.Match(number);
  207. return m.Success;
  208. }
  209. /// <summary>
  210. /// Url有效性
  211. /// </summary>
  212. /// <param name="url"></param>
  213. /// <returns></returns>
  214. static public bool IsValidURL(string url)
  215. {
  216. return Regex.IsMatch(url, @"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s]$");
  217. }
  218. /// <summary>
  219. /// IP有效性
  220. /// </summary>
  221. /// <param name="ip"></param>
  222. /// <returns></returns>
  223. public static bool IsValidIP(string ip)
  224. {
  225. return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
  226. }
  227. /// <summary>
  228. /// domain 有效性
  229. /// </summary>
  230. /// <param name="host">域名</param>
  231. /// <returns></returns>
  232. public static bool IsValidDomain(string host)
  233. {
  234. Regex r = new Regex(@"^\d+$");
  235. if (host.IndexOf(".") == -1)
  236. {
  237. return false;
  238. }
  239. return r.IsMatch(host.Replace(".", string.Empty)) ? false : true;
  240. }
  241. /// <summary>
  242. /// 判断是否为base64字符串
  243. /// </summary>
  244. /// <param name="str"></param>
  245. /// <returns></returns>
  246. public static bool IsBase64String(string str)
  247. {
  248. return Regex.IsMatch(str, @"[A-Za-z0-9\+\/\=]");
  249. }
  250. /// <summary>
  251. /// 验证字符串是否是GUID
  252. /// </summary>
  253. /// <param name="guid">字符串</param>
  254. /// <returns></returns>
  255. public static bool IsGuid(string guid)
  256. {
  257. if (string.IsNullOrEmpty(guid))
  258. return false;
  259. return Regex.IsMatch(guid, "[A-F0-9]{8}(-[A-F0-9]{4}){3}-[A-F0-9]{12}|[A-F0-9]{32}", RegexOptions.IgnoreCase);
  260. }
  261. #endregion
  262. #region 日期检查
  263. /// <summary>
  264. /// 判断输入的字符是否为日期
  265. /// </summary>
  266. /// <param name="strValue"></param>
  267. /// <returns></returns>
  268. public static bool IsDate(string strValue)
  269. {
  270. return Regex.IsMatch(strValue, @"^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))");
  271. }
  272. /// <summary>
  273. /// 判断输入的字符是否为日期,如2004-07-12 14:25|||1900-01-01 00:00|||9999-12-31 23:59
  274. /// </summary>
  275. /// <param name="strValue"></param>
  276. /// <returns></returns>
  277. public static bool IsDateHourMinute(string strValue)
  278. {
  279. return Regex.IsMatch(strValue, @"^(19[0-9]{2}|[2-9][0-9]{3})-((0(1|3|5|7|8)|10|12)-(0[1-9]|1[0-9]|2[0-9]|3[0-1])|(0(4|6|9)|11)-(0[1-9]|1[0-9]|2[0-9]|30)|(02)-(0[1-9]|1[0-9]|2[0-9]))\x20(0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9]){1}$");
  280. }
  281. #endregion
  282. #region 其他
  283. /// <summary>
  284. /// 检查字符串最大长度,返回指定长度的串
  285. /// </summary>
  286. /// <param name="sqlInput">输入字符串</param>
  287. /// <param name="maxLength">最大长度</param>
  288. /// <returns></returns>
  289. public static string CheckMathLength(string inputData, int maxLength)
  290. {
  291. if (inputData != null && inputData != string.Empty)
  292. {
  293. inputData = inputData.Trim();
  294. if (inputData.Length > maxLength)//按最大长度截取字符串
  295. {
  296. inputData = inputData.Substring(0, maxLength);
  297. }
  298. }
  299. return inputData;
  300. }
  301. /// <summary>
  302. /// 转换成 HTML code
  303. /// </summary>
  304. /// <param name="str">string</param>
  305. /// <returns>string</returns>
  306. public static string Encode(string str)
  307. {
  308. str = str.Replace("&", "&amp;");
  309. str = str.Replace("'", "''");
  310. str = str.Replace("\"", "&quot;");
  311. str = str.Replace(" ", "&nbsp;");
  312. str = str.Replace("<", "&lt;");
  313. str = str.Replace(">", "&gt;");
  314. str = str.Replace("\n", "<br>");
  315. return str;
  316. }
  317. /// <summary>
  318. ///解析html成 普通文本
  319. /// </summary>
  320. /// <param name="str">string</param>
  321. /// <returns>string</returns>
  322. public static string Decode(string str)
  323. {
  324. str = str.Replace("<br>", "\n");
  325. str = str.Replace("&gt;", ">");
  326. str = str.Replace("&lt;", "<");
  327. str = str.Replace("&nbsp;", " ");
  328. str = str.Replace("&quot;", "\"");
  329. return str;
  330. }
  331. #endregion
  332. }
  333. }