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

Regular Expression validation of a property in a model

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

我尝试了以下代码-

[RegularExpression(@"(^(?i:([a-z])(?!{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]