- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 RegularExpressionAttribute
来验证属性。该属性需要允许除零长度字符串、null 或(仅)空格之外的任何内容
。我使用的正则表达式是 "^(?!^ *$)^.+$"
.
如果值为 null
或 空字符串
,RegularExpressionAttribute.IsValid
总是返回 true 但我认为它应该为 false(只是空格工作正常)。
我不是正则表达式专家,但我相信表达式没问题(如果我使用 Regex.IsMatch
直接从我的代码验证正则表达式,空字符串将返回 false - 正如预期的那样)。这是 RegularExpressionAttribute 的问题吗?
(我知道 RequiredAttribute 通常会很快,但在这种情况下不是一个选项)
更新:
为了消除歧义,这里有一段简单的测试代码来演示:
public class MyValueTester
{
// internal const string _REGEX_PATTERN = "^(?!\\s*$).+$";
// internal const string _REGEX_PATTERN = @"^(?!\s*$)";
internal const string _REGEX_PATTERN = @"[^ ]";
public MyValueTester()
{
ProperValue = "hello";
NullValue = null;
SpaceValue = " ";
LeadingSpaceValue = " hi";
EmptyValue = "";
}
[RegularExpression(_REGEX_PATTERN, ErrorMessage = "To err is human")]
public string ProperValue { get; set; }
[RegularExpression(_REGEX_PATTERN, ErrorMessage = "To null is human")]
public string NullValue { get; set; }
[RegularExpression(_REGEX_PATTERN, ErrorMessage = "To space is human")]
public string SpaceValue { get; set; }
[RegularExpression(_REGEX_PATTERN, ErrorMessage = "To empty is human")]
public string EmptyValue { get; set; }
[RegularExpression(_REGEX_PATTERN, ErrorMessage = "To lead is human")]
public string LeadingSpaceValue { get; set; }
}
测试代码:
MyValueTester myValueTester = new MyValueTester();
ValidationContext validationContext = new ValidationContext(myValueTester);
List<ValidationResult> validationResults = new List<ValidationResult>();
Debug.WriteLine("=== Testing pattern '" + MyValueTester._REGEX_PATTERN + "' ===");
var expectedResults = new[]
{
new {propertyName = "ProperValue", expectedPass = true},
new {propertyName = "LeadingSpaceValue", expectedPass = true},
new {propertyName = "NullValue", expectedPass = false},
new {propertyName = "SpaceValue", expectedPass = false},
new {propertyName = "EmptyValue", expectedPass = false},
};
bool isMatch = Validator.TryValidateObject(myValueTester, validationContext, validationResults, true);
foreach (var expectedResult in expectedResults)
{
ValidationResult validationResult = validationResults.FirstOrDefault(r => r.MemberNames.Contains(expectedResult.propertyName));
string result = expectedResult.expectedPass ? (validationResult == null ? "Ok" : "** Expected Pass **") : (validationResult != null ? "Ok" : "** Expected Failure **");
Debug.WriteLine("{0}: {1}", expectedResult.propertyName, result);
}
到目前为止模式建议的结果:
=== Testing pattern '^(?!\s*$).+$' ===
ProperValue: Ok
LeadingSpaceValue: Ok
NullValue: ** Expected Failure **
SpaceValue: Ok
EmptyValue: ** Expected Failure **
=== Testing pattern '^(?!\S*$)' ===
ProperValue: ** Expected Pass **
LeadingSpaceValue: ** Expected Pass **
NullValue: ** Expected Failure **
SpaceValue: Ok
EmptyValue: ** Expected Failure **
=== Testing pattern '^(?!\s*$)' ===
ProperValue: ** Expected Pass **
LeadingSpaceValue: ** Expected Pass **
NullValue: ** Expected Failure **
SpaceValue: Ok
EmptyValue: ** Expected Failure **
=== Testing pattern '[^ ]' ===
ProperValue: ** Expected Pass **
LeadingSpaceValue: ** Expected Pass **
NullValue: ** Expected Failure **
SpaceValue: Ok
EmptyValue: ** Expected Failure **
最佳答案
令人沮丧的答案:尽管在 RegEx 等中工作正常,但未能使最简单的模式起作用后,我确信 RegularExpressionAttribute 中存在错误。事实证明,数据注释验证器简化了 RegEx 的实现,以阻止新手错误。 MSDN has this comment :
If the value of the property is null or an empty string (""), thevalue automatically passes validation for theRegularExpressionAttribute attribute. To validate that the value isnot null or an empty string, use the RequiredAttribute attribute.
所以从技术上讲不是错误 - 只是一个糟糕的主意。在我的例子中,解决方法是编写一个 RegularExpressionAttribute 扩展来处理空值/空(我可以通过使用适当的正则表达式在 RegularExpressionAttribute::IsValid 中验证来做到这一点。
关于c# - 带空/空的数据注释 RegularExpressionAttribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32842239/
我有一个用于客户端验证的字符串: private const String regex = @"^(?:\b(?:\d{5}(?:\s*-\s*\d{5})?|([A-Z]{2})\d{3}(?:\s
我需要验证的只是一个小数点(或一个点)。 我使用了以下正则表达式: "\.+" 我用 .NET 编译器和 JS 测试器进行了测试,表达式似乎是有效的。 我在模型中有以下属性: [RegularExpr
我正在使用这个正则表达式:@"[ \]\[;\/\\\?:*""<>|+=]|^[.]|[.]$" 第一部分[ \]\[;\/\\\?:*""<>|+=]应匹配括号内的任何字符。 下一部分 ^[.]如
我有以下代码来测试类属性: public class RegExtTest { [RegularExpression(@"^[A-z0-9]{6}$", ErrorMessage = "Lot
public class City { [DynamicReqularExpressionAttribute(PatternProperty = "RegEx")] public stri
我无法对使用 MVC3RTM 的正则表达式进行客户端验证。当我注释掉 RegularExpression 属性时,所有其他客户端验证都有效,所以我知道这是导致我出现问题的原因。 我有一个简单的模型。
我正在使用 RegularExpressionAttribute 来验证属性。该属性需要允许除零长度字符串、null 或(仅)空格之外的任何内容。我使用的正则表达式是 "^(?!^ *$)^.+$".
我需要一个匹配包含“[S]”的文本的正则表达式模式。我试过这样的事情: ^(?\\[S\\]$)$ 但它不起作用。我怎样才能做到这一点? 编辑: 正则表达式的目的是将其用作客户端验证中的正则表达式:
我们为 MVC3 应用程序编写自定义 RegularExpressionAttribute。自定义 RegularExpressionAttribute 的目的是,我们想用关键字替换资源文件消息中的
我使用 RegularExpressionAttribute像这样在我的 EF 元数据上: [RegularExpression("[A-Z]+")] public string Code { get
为了保持我的模型验证干净,我想实现我自己的验证属性,比如 PhoneNumberAttribute和 EmailAttribute .其中一些可以有利地实现为从RegularExpressionAtt
The Gu provides an example介绍如何创建覆盖 RegularExpressionAttribute 的自定义验证器。 这样做的好处是你不必create a custom Mod
我是一名优秀的程序员,十分优秀!