ASP.NET MVC 5:EmailAddress属性自定义错误消息 [英] ASP.NET MVC 5: EmailAddress attribute custom error message
本文介绍了ASP.NET MVC 5:EmailAddress属性自定义错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在注册表单中,我使用EmailAddress属性来验证用户电子邮件。
In register form I use EmailAddress attribute to validate user email.
public class RegisterViewModel
{
[Required(ErrorMessage = "Pole wymagane")]
[Display(Name = "Email")]
[DataType(DataType.EmailAddress)]
[EmailAddress]
public string Email { get; set; }
}
如果验证失败,是否有机会显示电子邮件地址出了什么问题?例如糟糕,我看到您的电子邮件地址包含空格
Is there any chance to show what is wrong with email address if validation fails? For example 'oops, I see that your email address contains whitespace'
推荐答案
您必须为此添加另一个验证。使用 [RegularExpression]
You have to add another validation for that. Example using [RegularExpression]
public class RegisterViewModel
{
[Required(ErrorMessage = "Pole wymagane")]
[RegularExpression(@"^\S*$", ErrorMessage = "Email Address cannot have white spaces")]
[Display(Name = "Email")]
[DataType(DataType.EmailAddress)]
[EmailAddress]
public string Email { get; set; }
}
这篇关于ASP.NET MVC 5:EmailAddress属性自定义错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文