TypeValidation.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. namespace ProjectBase.Util
  8. {
  9. public static class TypeValidation
  10. {
  11. /// <summary>
  12. /// 整数规则验证
  13. /// </summary>
  14. /// <param name="str"></param>
  15. /// <returns></returns>
  16. public static bool IsInteger(string str)
  17. {
  18. if (Regex.Match(str, "^[0-9]*$").Success)
  19. return true;
  20. else
  21. return false;
  22. }
  23. /// <summary>
  24. /// 数值型验证规则(1到3位小数)
  25. /// </summary>
  26. /// <param name="str"></param>
  27. /// <returns></returns>
  28. public static bool IsNumerical(string str)
  29. {
  30. if (Regex.Match(str, "^[0-9]+(.[0-9]{1,3})?$").Success)
  31. return true;
  32. else
  33. return false;
  34. }
  35. /// <summary>
  36. /// 是否为日期型字符串
  37. /// </summary>
  38. /// <param name="StrSource">日期字符串(2008-05-08)</param>
  39. /// <returns></returns>
  40. public static bool IsDate(string StrSource)
  41. {
  42. return Regex.IsMatch(StrSource, @"^((((1[6-9]|[2-9]\d)\d{2})/(0?[13578]|1[02])/(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})/(0?[13456789]|1[012])/(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})/0?2/(0?[1-9]|1\d|2[0-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))/0?2/29/))$");
  43. //return Regex.IsMatch(StrSource, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
  44. }
  45. /// <summary>
  46. /// 是否为时间型字符串
  47. /// </summary>
  48. /// <param name="source">时间字符串(15:00:00)</param>
  49. /// <returns></returns>
  50. public static bool IsTime(string StrSource)
  51. {
  52. return Regex.IsMatch(StrSource, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
  53. }
  54. /// <summary>
  55. /// 是否为日期+时间型字符串
  56. /// </summary>
  57. /// <param name="source"></param>
  58. /// <returns></returns>
  59. public static bool IsDateTime(string StrSource)
  60. {
  61. return Regex.IsMatch(StrSource, @"^(((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$ ");
  62. }
  63. }
  64. }