IsIPAddressAttribute.cs 708 B

1234567891011121314151617181920212223242526
  1. using System.ComponentModel.DataAnnotations;
  2. namespace Masuit.Tools.Core.Validator
  3. {
  4. /// <summary>
  5. /// 验证IPv4地址是否合法
  6. /// </summary>
  7. public class IsIPAddressAttribute : ValidationAttribute
  8. {
  9. public override bool IsValid(object value)
  10. {
  11. if (value is null)
  12. {
  13. ErrorMessage = "IP地址不能为空!";
  14. return false;
  15. }
  16. string email = value as string;
  17. if (email.MatchInetAddress())
  18. {
  19. return true;
  20. }
  21. ErrorMessage = "IP地址格式不正确,请输入有效的IPv4地址";
  22. return false;
  23. }
  24. }
  25. }