FormMethods.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. using System.Xml;
  10. namespace UAS_MES_Tools
  11. {
  12. internal static class FormMethods
  13. {
  14. public static bool IsCheckSNRules(string Rule,string SN,string RuleLeng)
  15. {
  16. if (SN.Length != Convert.ToInt32(RuleLeng)) return true;
  17. int RuleIndex = 0;
  18. int SNIndex = 0;
  19. while (RuleIndex < Rule.Length && SNIndex < SN.Length)
  20. {
  21. char RuleChar = Rule[RuleIndex];
  22. if (RuleChar == '{')
  23. {
  24. int endBraceIndex = Rule.IndexOf('}', RuleIndex);
  25. if (endBraceIndex == -1)
  26. {
  27. return true;
  28. }
  29. string content = Rule.Substring(RuleIndex + 1, endBraceIndex - RuleIndex - 1);
  30. if (content.Contains(","))
  31. {
  32. string[] allowedChars = content.Split(',');
  33. if (Array.IndexOf(allowedChars, SN[SNIndex].ToString()) == -1)
  34. {
  35. return true;
  36. }
  37. }
  38. else if (content == "YY")
  39. {
  40. if (SNIndex + 1 >= SN.Length) return true;
  41. string yearPart = SN.Substring(SNIndex, 2);
  42. if (!Regex.IsMatch(yearPart, @"^\d{2}$"))
  43. {
  44. return true;
  45. }
  46. SNIndex++;
  47. }
  48. else if (content == "MM")
  49. {
  50. if (SNIndex + 1 >= SN.Length) return true;
  51. string monthPart = SN.Substring(SNIndex, 2);
  52. if (!Regex.IsMatch(monthPart, @"^(0[1-9]|1[0-2])$"))
  53. {
  54. return true;
  55. }
  56. SNIndex++;
  57. }
  58. else if (content == "DD")
  59. {
  60. if (SNIndex + 1 >= SN.Length) return true;
  61. string dayPart = SN.Substring(SNIndex, 2);
  62. if (!Regex.IsMatch(dayPart, @"^(0[1-9]|[12]\d|3[01])$"))
  63. {
  64. return true;
  65. }
  66. SNIndex++;
  67. }
  68. else
  69. {
  70. return true;
  71. }
  72. RuleIndex = endBraceIndex + 1;
  73. SNIndex++;
  74. }
  75. else if (RuleChar == '*')
  76. {
  77. RuleIndex++;
  78. SNIndex++;
  79. }
  80. else
  81. {
  82. if (RuleChar != SN[SNIndex])
  83. {
  84. return true;
  85. }
  86. RuleIndex++;
  87. SNIndex++;
  88. }
  89. }
  90. if (RuleIndex != Rule.Length || SNIndex != SN.Length)
  91. {
  92. return true;
  93. }
  94. return false;
  95. }
  96. }
  97. }