Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Error Message not appear Data Annotation

I have a class on which applied code first data annotation but on MVC view, I am finding out Default error message appear instead of custom one. Any ssugesstion please

public class PaymentModel
    {
        [DisplayName("Email Address")]
        [Required]
        [RegularExpression(@"^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$", ErrorMessage = "Please enter a valid Email Address")]
        public string EmailAddress { get; set; }

        [DisplayName("Credit Card Number")]
        [Required]
        [StringLength(16, ErrorMessage = "The {0} must be at least {1} characters long.", MinimumLength = 3)]
        public int CreditCardNumber { get; set; }

        [Required]
        [StringLength(3, ErrorMessage = "The {0} must be at least {1} characters long.", MinimumLength = 3)]
        public int Cvv { get; set; }

        [DisplayName("Expiry Month")]
        [Required]
        public int ExpiryMonth { get; set; }

        [DisplayName("Expiry Year")]
        [Required]
        public int ExpiryYear { get; set; }
    }
like image 318
Ammar Khan Avatar asked May 13 '14 18:05

Ammar Khan


Video Answer


1 Answers

You need to specify an ErrorMessage explicitly like

[Required(ErrorMessage = "Email address is required")]
public string EmailAddress { get; set; }
like image 98
cgijbels Avatar answered Oct 22 '22 06:10

cgijbels