Categories
Uncategorized

ASP.NET MVC 4 Data Annotations by example.

1. Email Validation using regex

[Required(ErrorMessage = "Enter your email address.")]
[RegularExpression(@"^[w-]+(.[w-]+)*@([a-z0-9-]+(.[a-z0-9-]+)*?.[a-z]{2,6}|(d{1,3}.){3}d{1,3})(:d{4})?$", ErrorMessage = "Email is not valid")]
public string Email { get; set; }

2. Max and Min length check

[Required(ErrorMessage = "Enter your password.")]
[StringLength(50, MinimumLength = 4, ErrorMessage = "Password has to be between 4 to 50 characters.")]
public string Password { get; set; }