IPValidationRule.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. using System.Windows.Controls;
  9. namespace TeamAAS_VP.Identity
  10. {
  11. public class IPValidationRule:ValidationRule
  12. {
  13. public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
  14. {
  15. if (IsValidIP(value.ToString()))
  16. {
  17. return new ValidationResult(true, null);
  18. }
  19. else
  20. {
  21. return new ValidationResult(false, "Invalid_Address");
  22. }
  23. }
  24. private bool IsValidIP(string ip)
  25. {
  26. if (string.IsNullOrWhiteSpace(ip))
  27. return false;
  28. string[] parts = ip.Split('.');
  29. if (parts.Length != 4)
  30. return false;
  31. foreach (var part in parts)
  32. {
  33. if (!byte.TryParse(part, out byte b))
  34. return false;
  35. }
  36. return true;
  37. }
  38. }
  39. }