TCPPortValidationRule.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Globalization;
  3. using System.Windows.Controls;
  4. namespace TeamAAS_VP.ValidationRules
  5. {
  6. public class TCPPortValidationRule : ValidationRule
  7. {
  8. public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  9. {
  10. // 判断输入值是否为空
  11. if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
  12. {
  13. return new ValidationResult(false, "The TCP port cannot be empty.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
  14. }
  15. string portString = value.ToString();
  16. if (int.TryParse(portString, out int port))
  17. {
  18. // 检查端口号是否在有效范围内 (0-65535)
  19. if (port > 0 && port <= 65535)
  20. {
  21. return ValidationResult.ValidResult;
  22. }
  23. else
  24. {
  25. return new ValidationResult(false, "The TCP port must be between 1 and 65535".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
  26. }
  27. }
  28. else
  29. {
  30. return new ValidationResult(false, "Invalid TCP port format.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
  31. }
  32. }
  33. }
  34. }