简体   繁体   中英

Regular Expression validation of a property in a model

I am trying to validate a property which must have nine-digit code and cannot end with four zeros or four nines and must be entered without special characters.

I tried the following code-

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

But it's not working.

Example: exasdea0000 , asdea9999 , exasde@0000 or as_ea9999 can't be inputted.

How can I achieve this?

You can write your regex like this:

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

Explanation:

^                   // 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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