回顾:内置验证属性。
具体步骤
编写验证用的特性:
public class ValidEmailDomainAttribute : ValidationAttribute
{
private readonly string _allowedDomain;
public ValidEmailDomainAttribute(string allowedDomain)
{
_allowedDomain = allowedDomain;
}
public override bool IsValid(object value)
{
var strings = value.ToString().Split('@');
return strings[1].ToUpper() == _allowedDomain.ToUpper();
}
}
在 ViewModel 中使用验证特性:
public class RegisterViewModel
{
[Required(ErrorMessage = "邮箱地址不能为空")]
[EmailAddress]
[Display(Name = "邮箱地址")]
[Remote(action: "IsEmailInUse", controller: "Account")]
[ValidEmailDomain(allowedDomain: "52abp.com",ErrorMessage = "邮箱后缀必须是 52abp.com")]
public string Email { get; set; }
...
}
效果: