编程语言
首页 > 编程语言> > c# – 使用带正则表达式的DataAnnotations不匹配

c# – 使用带正则表达式的DataAnnotations不匹配

作者:互联网

是否可以使用DataAnnotation和正则表达式过滤文本框中的条目?即当字符串中的“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,regex,asp-net-mvc,entity-framework,data-annotations
来源: https://codeday.me/bug/20190624/1282857.html