c# - 将 DataAnnotations 与正则表达式一起使用以不匹配

标签 c# asp.net-mvc regex entity-framework data-annotations

DataAnnotation 是否可行? s 和正则表达式来过滤文本框中的条目?即当字符串中包含单词“apt”时触发无效响应?

最佳答案

是的,你可以使用负面环视来做到这一点,就像这样:

public class MyModel
{
    [RegularExpression(@"^((?!apt).)*$", ErrorMessage = "You can not have that")]
    public string MyValue { get; set; }
}

对于这些类型的正则表达式,这是一个很好的引用问题。

Regular expression to match string not containing a word?

关于c# - 将 DataAnnotations 与正则表达式一起使用以不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18218701/

相关文章:

asp.net - 将 ASP.NET 从 RC1 升级到核心后出现 Razor 异常

regex - 如何使用 ColdFusion 从 XML 字符串中删除所有多余的空格?

javascript - 如果是逗号,则从字符串中删除第一个字符

c# - 如何在 Unity3D 中实现类似于 Bonjour 浏览器的 DNS - 服务发现

c# - 在 WPF Viewport3D 中可视化点云非常慢

c# - 'Disabled ghost property fetching for <entity> because it does not support lazy at the entity level'的含义

c# - 将空子对象从父对象传递到局部 View

javascript - 如何在点击两次后隐藏文件上传按钮?

regex - 超过复杂正则子表达式递归限制 (32766)

c# - 如何在单独的 AppDomain 中运行方法?