I created my own attribute to check on the client side for a positive decimal value. The problem is that when I apply a regular expression directly to a property, it works fine, but when I use a custom attribute, it does not work.
Work mode:
[RegularExpression(@"^(?!0?(,0?0)?$)([0-9]{0,3}(,[0-9]{1,2})?)?$", ErrorMessage = "Largura inválida.")] [Required(ErrorMessage = "Largura obrigatória.")] [Display(Name = "Formato Aberto")] public decimal SizeOpenedWidth { get; set; }
User attribute:
public class PositiveDecimalAttribute : RegularExpressionAttribute { public PositiveDecimalAttribute() : base("^(?!0?(,0?0)?$)([0-9]{0,3}(,[0-9]{1,2})?)?$") { } }
Integrated in the property:
[PositiveDecimal(ErrorMessage = "Largura inválida.")] [Required(ErrorMessage = "Largura obrigatória.")] [Display(Name = "Formato Aberto")] public decimal SizeOpenedWidth { get; set; }
In the second case, the client-side check presents an error message:
The field Formato Aberto must be a number.
Do I need to integrate a new attribute when checking on the client side?
c # regex validation asp.net-mvc
Patrick
source share