c# - 模型中属性的正则表达式验证

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

我正在尝试验证一个属性,该属性必须具有九位代码并且不能以四个零或四个九结尾,并且必须在没有特殊字符的情况下输入。

我尝试了以下代码-

[RegularExpression(@"(^(?i:([a-z])(?!\1{2,}))*$)|(^[A-Ya-y1-8]*$)", ErrorMessage = "You can not have that")]
public string Test{ get; set; }

但它不起作用。

示例: exasdea0000asdea9999exasde@0000as_ea9999 可以'无法输入。

我怎样才能做到这一点?

最佳答案

你可以这样写你的正则表达式:

^(?!\d+[09]{4}$)\d{9}$

解释:

^                   // from start point
 (?!                // look forward to don't have
    .+              // some characters
    [09]{4}         // followed by four chars of 0 or 9
    $               // and finished
 )
 \d{9}              // nine characters of digits only
$                   // finished

[Regex Demo]

关于c# - 模型中属性的正则表达式验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45833465/

相关文章:

ruby - Gsub 导致部分字符串被替换

c# - 复制字典的意外结果

c# - 如何在C#中将手机号码转换为国际格式

c# - 类设计——如何管理多线程响应

python - python 正则表达式是否有不编译 r'(\s*)+' 的原因?

javascript - 在 Javascript 中使用正则表达式删除 HTML 注释

c# - 使用 Html.BeginForm 和 FormMethod.Get 传递另一个查询字符串参数

asp.net-mvc - ASP MVC 中的拖放问题 Kendo UI Web TreeView

ios - 为什么在操作中发布数据时,部分 View 的字段为空?

c# - F# GUI问题(将C#翻译成F#)