Ammar Khan
Ammar Khan

Reputation: 2585

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; }
    }

Upvotes: 7

Views: 8298

Answers (1)

cgijbels
cgijbels

Reputation: 6114

You need to specify an ErrorMessage explicitly like

[Required(ErrorMessage = "Email address is required")]
public string EmailAddress { get; set; }

Upvotes: 9

Related Questions