2017-08-23 48 views
1

我想驗證一個屬性必須有九位數的代碼,並且不能以四個零或四個九位結尾,並且必須輸入不帶特殊字符。模型中屬性的正則表達式驗證

我嘗試以下代碼 -

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

但它不工作。

實施例:exasdea0000asdea9999[email protected]as_ea9999不能被輸入。

我該如何做到這一點?

+2

你有樣本輸入或測試用例? –

+0

什麼是你的特殊字符?什麼是格式? –

+3

[this](https://regex101.com/r/rIU0aJ/1)有幫助嗎? –

回答

4

你可以寫你的正則表達式是這樣的:

^(?!\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]