Override Required Validation Attribute Error Message

Override Required validation attribute error message with custom error message.

If you want to replace the default error message for [Required] attribute that can be seen inside span tags as (data-val-number="The value '' is required") you have to use nullable data type. String is a nullable data type, but data types (like: int, decimal, float, double, DateTime) are not nullable, so even if decorated the property with [Required] attribute it will not be checked for null value (see Notes on the use of the Required attribute ).

So just adding (?) to the data type will make it nullable and will trigger null check.:

/* sample model */

        public class MyModel
            [Required(ErrorMessage = "You will not see me!")]
            public decimal Number1 { get; set; }

            [Required(ErrorMessage = "Please enter a valid decimal number...")]
            public decimal? Number2 { get; set; }

If you submit empty values the error message of Number1 will not appear, because it is non-nullable by default so it will not perform nullcheck, but Number2 will be checked because it is nullable.

Customize Required Attribute Error Message