.NET中的验证框架,可以在字段之间进行编辑(Validation Framework in .NET that can do edits between fields)
根据我的经验,.NET中的许多验证框架允许您一次验证一个字段,例如确保字段是邮政编码或电子邮件地址。 我通常将这些内部编辑称为。
在我的项目中,我们经常需要进行场间编辑。 例如,如果您有这样的类:
public class Range { public int Min { get; set; } public int Max { get; set; } }
您可能希望确保Max大于Min。 您可能还想对外部对象进行一些验证。 例如,鉴于你有一个这样的类:
public class Person { public string PostalCode { get; set; } }
无论出于何种原因,您希望确保邮政编码存在于数据库或提供给您的文件中。 我有更复杂的例子,比如用户提供数据字典,你想要针对该数据字典验证你的对象。
我的问题是:我们可以使用任何现有的验证框架(TNValidate,NHibernate Validator)用于.NET,还是我们需要使用规则引擎或者什么? 现实世界中的人们如何处理这种情况? :-)
From my experience many validation frameworks in .NET allow you to validate a single field at a time for doing things like ensuring a field is a postal code or email address for instance. I usually call these within-field edits.
In my project we often have to do between-field-edits though. For instance, if you have a class like this:
public class Range { public int Min { get; set; } public int Max { get; set; } }
you might want to ensure that Max is greater than Min. You might also want to do some validation against an external object. For instance given you have a class like this:
public class Person { public string PostalCode { get; set; } }
and for whatever reason you want to ensure that Postal Code exists in a database or a file provided to you. I have more complex examples like where a user provides a data dictionary and you want to validate your object against that data dictionary.
My question is: can we use any of the existing validation frameworks (TNValidate, NHibernate Validator) for .NET or do we need to use a rules engine or what?? How do you people in the real world deal with this situation? :-)
原文:https://stackoverflow.com/questions/2258513
最满意答案
你需要在最后添加一个负向前瞻:
^(?!0\.)(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.(?!0+$)\d{1,2})?$ ^^^^^^^
请参阅正则表达式演示
(?!0+$)
否定前瞻限制后续消费子模式\d{1,2})?$
以便\d{1,2}
不能再匹配00
或0
。You need to add a negative lookahead at the end:
^(?!0\.)(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.(?!0+$)\d{1,2})?$ ^^^^^^^
See the regex demo
The
(?!0+$)
negative lookahead restricts the subsequent consuming subpattern\d{1,2})?$
so that the\d{1,2}
cannot match00
or0
any longer.
相关问答
更多-
您可以使用Positive Lookahead而不是捕获组( ) ,它匹配空格字符或逗号,而不会使它们中的任何一个成为匹配的一部分。 var re = /.+[A-Z]{2}(?= |,)/ 查看Live demo You can use a Positive Lookahead instead of the capturing group ( ) which matches up to the space character or comma without making either of them ...
-
假设“邮政信箱123”中的尾随数字为“123”,则正则表达式确实匹配它们。 它甚至可以捕获反向引用的数字3。 正则表达式中存在大量缺陷。 例如,它匹配OST|||| 0 OST|||| 0 。 我可以建议我自己的PO Box正则表达式吗? ^(?:Post (?:Office )?|P[. ]?O\.? )?Box #?([0-9]+) 启用选项以在换行符处(通常为/m )进行^和$匹配,并启用不区分大小写的选项(通常为/i )。 不要启用自由间距选项(通常为/x )。 以下是它匹配的字符串的一些示例: ...
-
保持尾随零(Keeping trailing zeros)[2022-05-03]
如果这是为了打印目的, sprintf是你以后的: > sprintf("%.3f", round(5.2,3)) [1] "5.200" 有关格式化细节,请参阅?sprintf 。 If this is for printing purposes, sprintf is what you are after: > sprintf("%.3f", round(5.2,3)) [1] "5.200" See ?sprintf for formatting details. -
正则表达式删除美元符号和尾随零的价格输入(Regex to remove dollar symbol and trailing zeros for a price input)[2019-12-14]
你可以用 String result = s.replaceAll("^\\$|(?:(\\.[0-9]*[1-9])|\\.)0+$", "$1"); 请参阅正则表达式演示 。 细节 ^\\$ - 字符串开头的$ char | - 要么 (?: - 与两个替代方案中的任何一个匹配的非捕获组的开始: (\\.[0-9]*[1-9]) - 捕获第1组(后来用$1占位符引用):a . 接着是0+数字,然后是0以外的数字 | - 要么 \\. - 一个点 ) - 非捕获组的结束 0+ - 1+个零 $ - 结束 ... -
不漂亮,但完成工作 var str = " Some string "; var newStr = str.replace(/(^(\s+)|(\s+)$)/g,function(spaces){ return spaces.replace(/\s/g,"_");}); Not pretty, but gets the job done var str = " Some string "; var newStr = str.replace(/(^(\s+)|(\s+)$)/g,functi ...
-
使用RegEx我如何从十进制数中删除尾随零(Using RegEx how do I remove the trailing zeros from a decimal number)[2021-12-20]
正则表达式: ^(\d+\.\d*?[1-9])0+$ 要么 (\.\d*?[1-9])0+$ 替换字符串: $1 DEMO 码: var result:String = theStringOfTheNumber.replace(/(\.\d*?[1-9])0+$/g, "$1" ); Regex: ^(\d+\.\d*?[1-9])0+$ OR (\.\d*?[1-9])0+$ Replacement string: $1 DEMO Code: var result:String = the ... -
我建议你的函数版本使用LTrim与Replace结合而不是(昂贵的)正则表达式来计算DecPlaces的值。 DecPlaces的计算已成为“一线”。 其余代码是相同的,除了额外调用CDec以避免CStr在值很1.123642E-12返回科学记数法(如1.123642E-12 )。 Function Reduction(IValue As Double, EValue As Double) As String Dim TempPercent As Double Dim TempString ...
-
你可以尝试下面的正则表达式: (?i)\bvirtue('?s)?\b 此外,如果您计划经常使用正则表达式,建议使用常量以避免每次重新编译它,例如: private static final Pattern REGEX_PATTERN = Pattern.compile("(?i)\\bvirtue('?s)?\\b"); public static void main(String[] args) { String input = "virtue\nvirtues\nVIRT ...
-
你需要在最后添加一个负向前瞻: ^(?!0\.)(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.(?!0+$)\d{1,2})?$ ^^^^^^^ 请参阅正则表达式演示 (?!0+$)否定前瞻限制后续消费子模式\d{1,2})?$以便\d{1,2}不能再匹配00或0 。 You need to add a negative lookahead at the end: ^(?!0\.)(?:\d{1,3}(?:,\d ...
-
确保您在测试的数字周围有引号。 语法是: RegExpObject.test(string) var test = new RegExp(/ [0-9] {4,100} $ /); 返回false,因为它被解释为数字9: test.test(000009); 前导零被忽略。 返回true,因为整个字符串被认为是: test.test('000009'); Make sure you have quotes around the number you're testing. The syntax is: ...