[英]Custom Regular Expression not validating on the client side
I have build a custom attribute to validate on the client side a positive decimal value. 我已经建立了一个自定义属性,以在客户端验证正的十进制值。 The problem is that when I apply the regular expression directly to the property, it works fine, but when I use the custom attribute, it does not work.
问题是,当我将正则表达式直接应用于属性时,它可以正常工作,但是当我使用custom属性时,它不起作用。
Working 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; }
Custom 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 one, client side validation presents the error message: 在第二个中,客户端验证显示错误消息:
The field Formato Aberto must be a number.
Do I need to integrate the new attribute on the client side validation? 我是否需要在客户端验证中集成新属性?
You need to register your attribute in global.asax
. 您需要在
global.asax
注册您的属性。 In its Application_Start()
method, add the following code: 在其
Application_Start()
方法中,添加以下代码:
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(PositiveDecimalAttribute), typeof(RegularExpressionAttributeAdapter));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.