Custom regex not checked on client side

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?

0
c # regex validation asp.net-mvc
source share
1 answer

You need to register your attribute in global.asax . In your Application_Start() method, add the following code:

 DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(PositiveDecimalAtt‌​ribute), typeof(RegularExpressionAttributeAdapter)); 
+1
source share

All Articles