Home > OS >  Model validation using DataAnnotations [DataType(DataType.Currency]
Model validation using DataAnnotations [DataType(DataType.Currency]

Time:09-26

I'm trying to validate model with custom error message:

[Display(Name = "Iznos Prihoda")]
[Required(ErrorMessage = "Molimo unesite iznos prihoda!")]
[DataType(DataType.Currency, ErrorMessage = "Molimo unesite ispravni format valute!")]
public decimal IznosPrihod { get; set; }

And I'm getting the default error message

The field Iznos Prihoda must be a number

CodePudding user response:

Try this

[Display(Name = "Iznos Prihoda")]
[Required(ErrorMessage = "Molimo unesite iznos prihoda!")]
[DataType(DataType.Currency)]
[RegularExpression(@"^\d .?\d{0,2}$", ErrorMessage = ""Molimo unesite ispravni format valute!")]
  • Related