ASP.NET mvc 2 - How to add a "confirm your e-mail address" field?
Let's say I have a model that looks like this:
public class MyModel
{
[DisplayName("Email:")]
[Required(ErrorMessage = "Email is required")]
[Email(ErrorMessage = "Email is invalid")]
public string Email { get; set; }
}
In ASP.NET MVC 2, I'd render the text box and validation like so:
<%=Html.LabelFor(x => x.Email)%>
<%=Html.TextBoxFor(x => x.Email)%>
<%=Html.Valida开发者_StackOverflow社区tionMessageFor(x => x.Email)%>
How do I add a second field to allow the user to confirm their email address using the display name and validation from the model's property?
Decorate your Class with the following Attribute:
[PropertiesMustMatch("Email", "ConfirmEmail", ErrorMessage = "The Email Address and confirmation Email Address do not match.")]
public class MyModel
{
[DisplayName("Email:")]
[Required(ErrorMessage = "Email is required")]
[Email(ErrorMessage = "Email is invalid")]
public string Email { get; set; }
[DisplayName("Confrim Email:")]
[Required(ErrorMessage = "Email is required")]
[Email(ErrorMessage = "Email is invalid")]
public string ConfirmEmail { get; set; }
}
精彩评论