在asp.net条件RegularEx pression验证 [英] Conditional RegularExpression validators in asp.net

查看:95
本文介绍了在asp.net条件RegularEx pression验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

得到了与有数据注释验证属性特性的模型。一个典型的例子是:

  [RegularEx pression(^ [A-Z] {5} $的ErrorMessage =必填字段,必须有正确的格式)]
 公共字符串SomeProperty {获取;设置; }

我需要这些验证条件:如果模型中的一个特定的属性具有一定的价值,大多数验证应禁用。 -on无论是服务器端和客户方。 (
(我使用的是标准的AJAX女士客户端验证)

还有让数据注释验证器的条件,所以我已经看了看四周为实现新型数据注解验证的一些库没有默认的方式。
在万无一失的看着。codeplex.com和RequiredIf验证属性。
但是我发现,我要么不能正确实现他们或他们是在执行过于简单(只万无一失让你检查一个条件)

对我来说,最好的解决办法是,如果我能提供验证有两个参数:一个条件前pression和验证程序。可能是这个样子:

  [RequiredIf(OtherProperty ==真,RegularEx pression = @^ [AZ] {5} $的ErrorMessage =必填字段,必须有正确的格式)]
 公共字符串SomeProperty {获取;设置; }

解决方案,我可以尝试的你推荐任何其他有图书馆,或其他类型的?


解决方案

看起来你要使用的 RegularEx pressionIf 万无一失验证。

Got a model with properties that have Data Annotation Validator attributes. A typical example would be:

 [RegularExpression("^[a-z]{5,}$", ErrorMessage="required field, must have correct format")]
 public string SomeProperty {get;set; }

I need to make these validators conditional: If a specific property in the model has a certain value, most validators should be disabled. -On both the serverside and clientside. ( (I'm using standard Ms Ajax client side validation)

There's no default way of making Data Annotation Validators conditional so I've looked around for some libraries that implement new kinds of Data annotation validators. Looked at Foolproof.codeplex.com and the RequiredIf Validation Attribute. But I found that I either couldn't implement them properly or they were too simple in their implementation (foolProof only lets you check a single condition)

The best solution for me would be if I could provide the validator with 2 parameters: A conditional expression And a validator. Could look something like this:

 [RequiredIf("OtherProperty == true", RegularExpression=@"^[a-z]{5,}$", ErrorMessage="required field, must have correct format")]
 public string SomeProperty {get;set; }

Any there other libraries you recommend, or other types of solutions I could try?

解决方案

Looks like you want to use the RegularExpressionIf validator from foolproof.

这篇关于在asp.net条件RegularEx pression验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆