自定义的正前pression没有验证在客户端 [英] Custom Regular Expression not validating on the client side
问题描述
我有建立一个自定义属性在客户端验证了积极的十进制值。问题是,当我直接应用常规前pression的财产,它工作正常,但是当我使用自定义属性,这是行不通的。
工作方式:
[RegularEx pression(@^(?0?(0?0)?$)([0-9] {0,3}([ 0-9] {1,2})?)?$的ErrorMessage =Largurainválida。)
[必需(的ErrorMessage =Larguraobrigatória。)
[显示(NAME =Formato Aberto)]
公共小数SizeOpenedWidth {搞定;组; }
自定义属性:
公共类PositiveDecimalAttribute:RegularEx pressionAttribute
{
公共PositiveDecimalAttribute():基地(^(?!0(0 0)$)([0-9] {0,3}([0-9] {1,2}))????? $){}
}
集成在属性:
[PositiveDecimal(的ErrorMessage =Largurainválida。)
[必需(的ErrorMessage =Larguraobrigatória。)
[显示(NAME =Formato Aberto)]
公共小数SizeOpenedWidth {搞定;组; }
在第二个,客户端验证presents错误消息:
现场Formato Aberto必须是数字。
我是否需要新的属性在客户端验证集成?
您需要的Global.asax
注册您的属性。在其的Application_Start()
方法,添加以下code:
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.
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
. In its Application_Start()
method, add the following code:
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(PositiveDecimalAttribute), typeof(RegularExpressionAttributeAdapter));
这篇关于自定义的正前pression没有验证在客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!