IsEmailAttribute.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System.ComponentModel.DataAnnotations;
  2. namespace Masuit.Tools.Core.Validator
  3. {
  4. /// <summary>
  5. /// 邮箱校验
  6. /// </summary>
  7. public class IsEmailAttribute : ValidationAttribute
  8. {
  9. /// <summary>
  10. /// 邮箱校验
  11. /// </summary>
  12. /// <param name="value"></param>
  13. /// <returns></returns>
  14. public override bool IsValid(object value)
  15. {
  16. if (value == null)
  17. {
  18. ErrorMessage = "邮箱不能为空!";
  19. return false;
  20. }
  21. var email = value as string;
  22. if (email.Length < 6)
  23. {
  24. ErrorMessage = "您输入的邮箱格式不正确!";
  25. return false;
  26. }
  27. if (email.Length > 256)
  28. {
  29. ErrorMessage = "邮箱长度最大允许255个字符!";
  30. return false;
  31. }
  32. if (email.MatchEmail())
  33. {
  34. return true;
  35. }
  36. ErrorMessage = "您输入的邮箱格式不正确!";
  37. return false;
  38. }
  39. }
  40. }