繁体   English   中英

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

[英]Regular Expression validation of a property in a model

我正在尝试验证必须具有九位数代码且不能以四个零或四个九结尾且必须输入的不带特殊字符的属性。

我尝试了以下代码-

[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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM