回顾:内置验证属性

示例:注册时邮箱后缀必须是 52abp.com。
image.png

具体步骤

编写验证用的特性:

  1. public class ValidEmailDomainAttribute : ValidationAttribute
  2. {
  3. private readonly string _allowedDomain;
  4. public ValidEmailDomainAttribute(string allowedDomain)
  5. {
  6. _allowedDomain = allowedDomain;
  7. }
  8. public override bool IsValid(object value)
  9. {
  10. var strings = value.ToString().Split('@');
  11. return strings[1].ToUpper() == _allowedDomain.ToUpper();
  12. }
  13. }

在 ViewModel 中使用验证特性:

  1. public class RegisterViewModel
  2. {
  3. [Required(ErrorMessage = "邮箱地址不能为空")]
  4. [EmailAddress]
  5. [Display(Name = "邮箱地址")]
  6. [Remote(action: "IsEmailInUse", controller: "Account")]
  7. [ValidEmailDomain(allowedDomain: "52abp.com",ErrorMessage = "邮箱后缀必须是 52abp.com")]
  8. public string Email { get; set; }
  9. ...
  10. }

效果:
image.png